Reputation: 583
1) Whether C# interfaces and objective c @prototypes are same? and when the function inside the prototype are optional and when they are compulsory.
2) What is meaning of @property (nonatomic, retain)
I am new to objective c environment ,struck in these concepts.Please help me to solve this.
Upvotes: 2
Views: 900
Reputation: 3915
There is a huge difference that people are overlooking:
You can declare variables of type Interface in C# (as in Java) but you cannot declare variables of type protocol.
Upvotes: 0
Reputation: 12979
Q: Whether C# interfaces and objective c @prototypes are same? and when the function inside the prototype are optional and when they are compulsory.
A: These are basically the same. Objective-C uses protocols like C# uses interfaces. By default all methods that you list are required (meaning the compiler will complain if it doesn't see the object implement the method, but the program will still compile).
Q: What is meaning of @property (nonatomic, retain)
A: @property
means you are declaring a property. nonatomic
means that the reading/writing of the property will not be thread safe, but it makes it much faster. If you need a thread safe property, you must use atomic
(for which you simply leave out nonatomic
, as atomic
is the default. retain
means that the retainCount automatically increases when you set the property, so you don't have to perform the [someVariable retain]
call yourself. This has major memory management implications, so that's why you will frequently see a call to synthesize with an underscored ivar like so: @synthesize myObject = _myObject;
Upvotes: 2
Reputation: 7274
1) I assume you mean protocols. Protocols are close to interfaces in C# (and Java) but the semantics differ in that the method receiving the message does not need to implement the method. Then the message is ignored. Also sending messages (i.e. calling methods in C#) can be done on nil (i.e. null in C#) and nothing will happen.
2) @property(nonatmoic, retain) is the declaration of a property (which is a pair of methods, one getter and one setter). They can be automatically implemented using the @synthesize keyword. nonatomic is that no thread safety should be implemented. retain is that the objects reference count should be incremented/decremented in the setter.
Upvotes: 1
Reputation: 18488
1) I believe you are referring to protocols, formal protocols those woth the @protocol notation, are almost exactly the same as a C# interface, the diferrence ks that the protocol allows you to specify some optional methods, i.e. Some methods that you are not forced to implement.
2) A property is the equivalent of an instance variable for the class, and it can be @synthetized to automatically generate the setter and getters for this property. It can be declared atomic or non atomic, which means if the setter and getters will be performed as a singlw operation or not. It can also be declared as retain, copy or assign. Retain means that the ivar will send a retain msg to the obj in the generated setter, copy means that the setter will clone the instance and then send it a retain msg, while assign means that the value will just be assigned without a retain msg.
Hope that helps.
Upvotes: 0