2

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.

Shankaranarayana
  • 583
  • 1
  • 6
  • 10

4 Answers4

2

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;

FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82
  • thanks for the reply,will you please suggest any book which contains all these basic concepts.I searched in google but i didnt get a book with these concepts. – Shankaranarayana Aug 01 '11 at 06:01
  • Any beginner Obj-C book should cover the information on `@property`. `@protocols` should also be covered, as they touch a large percent of classes in Obj-C. I haven't read any Obj-C books; I prefer to learn via the internet. Obj-C is changing, so some printed information is out of date or incomplete. [This page](http://www.otierney.net/objective-c.html), while fairly short, touches on a lot of the basics of Obj-C, including memory management and protocols. – FreeAsInBeer Aug 01 '11 at 11:59
  • Neither `atomic` nor `nonatomic` are thread safe: see this answer: http://stackoverflow.com/questions/12347236/which-is-threadsafe-atomic-or-non-atomic – pgpb.padilla Sep 19 '13 at 23:38
1

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.

Anders Zommarin
  • 7,094
  • 2
  • 25
  • 24
  • thanks for the reply,will you please suggest any book which contains all these basic concepts.I searched in google but i didnt get a book with these concepts. – Shankaranarayana Aug 01 '11 at 06:01
0

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.

Καrτhικ
  • 3,833
  • 2
  • 29
  • 42
  • What do you mean by declaring "variables of type interface"? – Kirk Woll May 18 '12 at 17:56
  • @KirkWoll, i am sure kabram refers to something like IMyInterface c; Which means that, say, public class Implementation : IMyInterface can be submitted in c= Implementation(); I am, however, wondering how do we achieve the same in Obj-C – zaitsman May 27 '13 at 12:49
  • 1
    You define an id type genericised to the interface. id – Καrτhικ May 28 '13 at 13:15
0

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.

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
  • thanks for the reply,will you please suggest any book which contains all these basic concepts.I searched in google but i didnt get a book with these concepts. – Shankaranarayana Aug 01 '11 at 06:02