Objective-C 2.0 and #interface/#implementation - objective-c-2.0

I have written code such as the following, which by assumption shouldn't compile. I'm assuming that it shouldn't compile because the instance methods are not declared in the interface. Is this necessary, either way what's the logic behind it?
Thanks :-)
#interface Foo: NSObject
{
}
#end
#implementation Foo
-(void) blank
{
NSLog(#"Hey this works");
}
-(void) foo
{
NSLog(#"Strange");
}
#end

What is the question here? This code compiles just fine as methods do not need to be declared in the interface.

Related

Kotlin object vs companion-object vs package scoped methods

I have written this methods in Kotlin and analysed the bytecode:
Situation 1
class A {
object b {
fun doSomething() {}
}
}
Situation 2
class A {
companion object b {
fun doSomething() {}
}
}
Situation 3
fun doSomething() {}
Bytecode Result
Situation 1: class Test$asb, public final doSomething()I
Situation 2: class Test$Companion, public final doSomething()I
Situation 3: class TestKt, public final static doSomething()I
My questions are:
I have an enum class, and I want to return an enum instace given an enum variable, for instance, findById (enum(id, color)). How would I do it? Companion Object? object?
It seems the only way to have a real static method is in package level, without class declaration. But that becomes a little bit too global. Is there any way to access it via: ClassName.staticMethod, staticMethod being really static.
Provide meaningfull examples of package declaration methods, companion object and object.
Context. I have been coding in Kotlin and I find it amazing. But sometimes I need to make a decision: for example, a heavy immutable property which in java I would declare as static final, but in Kotlin I find it hard to "find an equivalent".
If you have a function which performs some action closely related to a class but doesn't require a class instance, such as your findById example, you should put it in the companion object of the class.
If you want to expose a method as a static method to Java code, you can annotate it with the #JvmStatic annotation.
If a function does not require an instance of a class, then it is your design decision where to put it. Use package level if it is package-specific, use a class companion if it closely relets to the class (for example other classes in the package have similar functions).
Note that enum has several in-build properties and patterns:
enum class Colour(val value: Int) {
black(100), red(200), green(300)
}
fun colourById(id: Int) = Colour.values[id]
fun colourByValue(value: Int) = Colour.values.first {it.value == value}
fun colourByName(name: String) = Colour.valueOf(name)
I would suggest to develop voddan answer:
enum class Color {
RED,
BLUE,
GREEN;
companion object Utils {
fun findById(color: Color): Color {
return color;
}
}
}
And to test
#Test
fun testColor() {
println(Color.Utils.findById(Color.valueOf("RED")));
}

Static and Normal class combined in one class

I am trying my best to explain the situation. I hope, what I wrote, is understandable.
We already have class defined like
public ref class TestClass
{
public:
TestClass();
virtual ~TestClass();
protected:
Car* m_car;
}
TestClass is managed C++ and Car is unmanaged C++.
So far so good, but now I need to make static object of TestClass also. So I modify the code like below
public ref class TestClass
{
private:
static TestClass^ s_test = nullptr ;
public:
TestClass();
virtual ~TestClass();
static TestClass^ Instance();
protected:
Car* m_car;
}
When I want to use static instant of the class, I just get it from calling
TestClass staticobj = TestClass::Instance();
Elsewhere, just call
TestClass normalobj = gcnew TestClass();
Instance function is creating s_test static object and returns it.
TestClass ^ TestClass::Instance()
{
if(s_test == nullptr)
{
s_test = gcnew TestClass();
s_test->m_car = new Car();
}
return s_test;
}
Is it a good approach?
Is there any other better approach to accomplish same thing?
Edit :
FYI Above code works.
I combined Krizz and Reed Copsey’s solutions. That solve independent Singleton and memory leak.
Here is my sample code,
Special Singleton class derived from test class,
public ref class SpecialSingletonTestClass: public TestClass
{
private:
static SpecialSingletonTestClass ^ s_ SpecialSingletonTestClass = nullptr;
public:
SpecialSingletonTestClass ();
static SpecialSingletonTestClass ^ Instance();
};
Changed the testclass so it has now one more finalizer function.
public ref class TestClass
{
public:
TestClass ();
virtual ~ TestClass ();
! TestClass ();
protected:
Car* m_car;
}
I tested above pattern , it worked.
Thanks you guys,
L.E.
Is it a good approach?
I would probably not consider this a good approach, as you're making a single class both a singleton and a normal class that you can instance directly.
Typically, if you need a singleton, this would preclude the need or desire to be able to instantiate the class.
If you truly need to have a way to have a "global" instance of this class, I would encapsulate that in a separate class which implements the singleton. This would, at least, make it clear that you are dealing with something that's a single instance in that case. I would not mix both use cases into a single class.
Well, actually there is an issue with memory leaks in your code.
You declare only virtual ~TestClass(); which, for managed classes, are internally turned by C++/CLI compiler into implementation of IDisposable.Dispose().
Therefore, if you put delete car into it, it will be called only if you delete test_class or, e.g. wrap into using (TestClass tst) {} block when using from C#.
It will not be called when object is GCed!
To be sure it is called you need to add finalizer to your class !MyClass(); which is turned by compiler into virtual void Finalize() and thus non-deterministically called when GC is freeing an object.
And it is the only way to free m_car of singleton object.
Therefore, I suggest:
TestClass()
{
m_car = new Car();
}
~TestClass()
{
if (m_car)
delete m_car;
m_car = NULL;
}
!TestClass()
{
if (m_car)
delete m_car;
m_car = NULL;
}
I'm unsure as to what situation you could possibly be in that would require both singleton-style semantics and normal creation semantics for the same class.
As far as what you've coded though, it looks completely fine. My only comments would be that your Instance() function shouldn't need to perform construction on Car, the Instance() function should just call the default constructor of TestClass which should do all that.
EDIT
In reference to:
#crush . The class is already define i just need to get static object of it. Singleton means only one object of the class, but in this case, class have multiple normal object. But i want to use only one object of this class for one specific goal only for limited period of time. – L.E. 2 mins ago
A singleton is (usually) a sign of bad design - alot of people call it an anti-pattern actually. Chances are if you just need this one single specific instance of this class for a limited period of time there are some issues:
Singleton design is made for static-style existence - the variable will live for the scope of your program after lazily initialized.
Allowing global access will move your code towards spaghetti logic. You'd be better off dynamically allocating the one you need and passing the pointer to it to where you need it to be. A shared_ptr would be good for this.
You should find a way around the singleton-style implementation in this case even if it's more work for you - it'll almost certainly be better design.

Declaring an array of UIImageViews globally in Xcode

I created an array of ImageViews in a Buttonclicked method:
UIImageView *arrayOfImageViews[10]
I successfully loaded them on the screen using a loop. (Big accomplishment for this beginner!)
But I want to refer to them in other methods (e.g. touchMove).
Where do I declare arrayOfImageViews to be both an array and a class of UIImageView so that I can use them in other methods? From what I can find, I'm to declare them in the .h and above Implementation in the .m. But I can't figure out the code and location to define the object as an array of UIImageViews in anything but the original function.
Try using a property. So in the .h file, you want to define the property something like this:
#interface MyClass: UIView {
NSArray *arrayOfImageViews;
}
#property (nonatomic,retain) NSArray *arrayOfImageViews;
Now in the implementation (.m file) You need to bind this property like this
#implementation MyClass
#synthesize arrayOfImageViews;
//now you need to initialize the array of images like this (look for the method viewDidLoad or //just add it)
- (void)viewDidLoad
{
NSMutableArray *tmpImages = [[NSMutableArray alloc]init];
//initialize images here
[self setArrayOfImageViews:tmpImages];
[tmpImages release];
[super viewDidLoad];
}
That should do it... Sorry I didn't have time to post code that actually compiles but it should help. Basically now you have a property on the class and you can access it like self.arrayOfImages, or even just arrayOfImages (from within MyClass). Also, I used NSArray which I suggest you do too. However you can also use UIImageView* arrayOfImages if you prefer.

Filling a NSComboBox with data generated in foreign-class-array

after my last question, regarding accessing an array from a different class, I ran into an new problem, that's giving me a headache for three days now. Everytime I think I have the correct solution approach, I fail.
Well... I don't have many experience yet regarding Cocoa Programming. But maybe you are able to give me the missing hint.
Let me show you what approach I've chosen:
1) the declaration of an array in the class PortConnection.h/.m
#interface PortConnection : NSObject {
#private
NSMutableArray *baudArray;
}
#property (nonatomic, retain) NSMutableArray *baudArray;
and the synthesize in .m
#implementation PortConnection
#synthesize baudArray;
Next I decided to implement a method in the ViewController that should be in charge of filling the array with data I need for display. The name of the class is "PortTableViewController.h"
#import "PortConnection.h"
#interface PortTableViewController : NSObject <NSTableViewDataSource, NSComboBoxDataSource> {
#private
IBOutlet NSComboBox *baudSelection;
PortConnection *portConnection;
}
#property (assign) IBOutlet NSTableView *portTableView;
- (IBAction)fillBaudSelection:(id)sender;
#end
and the implementation of my method "fillBaudSelection".
- (IBAction)fillBaudSelection:(id)sender {
int baudCount = [portConnection.baudArray count];
int i;
for (i = 0; i <= baudCount; i++){
[baudSelection addItemWithObjectValue:[portConnection.baudArray objectAtIndex:i]];
}
}
Furthermore I implemented the delegate methods for the combobox.
- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index{
return [portConnection.baudArray objectAtIndex:index];
}
- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox{
return [portConnection.baudArray count];
}
My questions are:
1) Do I need to use the Delegate Methods for a combo box at all?
2) the Combobox isn't filled with data at all, though the array is filled with data
3) Am I thinking to complicated??
Thanks so much for every hint I get from you!
best Regards
Sebastian
Are you sure you hooked the combobox correctly? make sure the delegate and the datasource are set to whatever class has the methods implemented.

C++/CLI - syntax for referencing static method from certain namespace

I want to execute a static method from certain class in certain namespace, but I have a problem with using it as a method parameter.
Example:
Lets say there is a class:
namespace ExampleNamespace {
public ref class A
{
public:
static int MethodA();
};
}
And I want to use MethodA in other namespace as a other's method parameter:
MethodB(MethodA());
Only way I can make it work is by writing it like this:
ExampleNamespace::A^ a;
MethodB(a->MethodA());
Is there a way to write it without that 'a' declaration before?
Something like
MethodB(ExampleNamespace::A->MethodA())
wont work...
Thank you in advance.
MethodB(ExampleNamespace::A::MethodA());

Resources