Sunday, 8 November 2015

Interview Queations 5 Attributs

1) What is atomic and nonatomic? Which one is safer? Which one is default?

            Both are same unless we use our setter and getter methods. If we create our own setter and getter method, then atomic is useful in thread processing, but it is slow. Nonatomic does not give guarantee on thread processing but it is fast.

2) What is the difference between copy & retain? When can we go for copy and when can we go for retain? 

      Retaining and copying are two different things, the first is conceptually call-by-reference while the second is call-by-value.

 

3) What is @synthesize?
@property and @synthesize provides the getter and setter (accessors) methods rather than you having to write it out yourself. The declaration is made with @property and it is implemented with @synthesize.
4) Explain deep copy and shallow copy?
newArary = oldArray isn't a copy at all. You end up with two pointers pointing to the exact same array.
newArray = [NSMutableArray arrayWithArray:oldArray]; is a shallow copy. You end up with two distinct arrays, so if you were to remove or add items from one array, it wouldn't affect the other array. However, the items in the two arrays are identical. If the first element of oldArray were an NSMutableDictionary and you added a key to it, you'd see that change on the first element of newArray as well (since those two objects are the same).
To do a deep copy, you would have to make a new array, and each element of the new array would be a deep copy of the corresponding element of the old array. (Yes, that definition is recursive).
5) Difference between retain and copy?
In a general setting, retaining an object will increase its retain count by one. This will help keep the object in memory and prevent it from being blown away. What this means is that if you only hold a retained version of it, you share that copy with whomever passed it to you.
Copying an object, however you do it, should create another object with duplicate values. Think of this as a clone. You do NOT share the clone with whomever passed it to you.
When dealing with NSStrings in particular, you may not be able to assume that whoever is giving you an NSString is truly giving you an NSString. Someone could be handing you a subclass (NSMutableString, in this case) which means that they could potentially modify the values under the covers. If your application depends on the value passed in, and someone changes it on you, you can run into trouble.
Retaining and copying are two different things, the first is conceptually call-by-reference while the second is call-by-value.
If you ask me, copy should be used for NSString. If it's Mutable, then it gets copied. If it's not, then it just gets retained. Exactly the semantics that you want in an app (let the type do what's best).

6) What is atomic and non atomic?
Basically, if you say nonatomic, and you generate the accessors using @synthesize, then if multiple threads try to change/read the property at once, badness can happen. You can get partially-written values or over-released/retained objects, which can easily lead to crashes. (This is potentially a lot faster than an atomic accessor, though.)
If you use the default (which is atomic; there's no keyword for this), then the @synthesized methods use an object-level lock to ensure that multiple reads/writes to a property are serialized. As the Apple docs point out, this doesn't mean the whole object is thread-safe, but the property reads/writes are.
Of course, if you implement your own accessors rather than using @synthesize, I think these declarations do nothing except express your intent as to whether the property is implemented in a threadsafe manner.

7) NULL or nil?
In Cocoa both of them are typedef to zero. nil is an object pointer, whereas NULL is a c pointer.  If you want to set char * to 0 then you would set it to NULL and if you want to set a view controller to 0 then you would set it to nil. Cocoa also gives us another typedef  called “Nil” (notice the case) which is class pointer. Now you know when to use the correct null pointer.

8) What exactly does @synthesize do?
@synthesize keyword to tell the compiler that it should generate the setter and/or getter methods for the property if you do not supply them within the @implementation block.
9) What is atomic and non-atomic?
nonatomic -  if  you generate the accessors using @synthesize, then if multiple threads try to change/read the property at once, badness can happen. You can get partially-written values or over-released/retained objects, which can easily lead to crashes. (This is potentially a lot faster than an atomic accessor, though.)  
atomic – If you use the default (which is atomic; there’s no keyword for this), then the @synthesized methods use an object-level lock to ensure that multiple reads/writes to a property are serialized. As the Apple docs point out, this doesn’t mean the whole object is thread-safe, but the property reads/writes are. Of course, if you implement your own accessors rather than using @synthesize, I think these declarations do nothing except express your intent as to whether the property is implemented in a threadsafe manner.



 

 

 

No comments:

Post a Comment