iOS Development Basics

1. Whats wrong with this code?
NSArray *array = @[@0, @1, @2, @3, @4, @5];
NSInteger count = 0;
[array enumeratedObjectsUsingBlock:^(NSNumber *number, NSUInteger idx, BOOL *stop){
if ([number compare:@2] == NSOrderedAscending){
count++;
stop = YES; !}
}];

Variable count is declared outside the block and it not assignable.
Assign BOOL variable pointer a BOOL value.
Solution:
__block NSInteger count = 0;
[array enumeratedObjectsUsingBlock:^(NSNumber *number, NSUInteger idx, BOOL *stop){
if ([number compare:@2] == NSOrderedAscending){
count++;
*stop = YES; }
}]

2. What does the @dynamic keyword mean in objective-c and where is it typically used?
Using the @dynamic directive essentially tells the compiler “don’t worry about it, a method is on the way.”
Some accessors are created dynamically at runtime, such as certain ones used in CoreData’s NSManagedObject class. If you want to declare and use properties for these cases, but want to avoid warnings about methods missing at compile time, you can use the @dynamic directive instead of @synthesize.

The @dynamic keyword tells the compiler that you will provide accessor methods dynamically at runtime. This can be done using the Objective-C runtime functions. Typically, you would use @dynamic with things like Core Data, where Core Data will provide the accessors based on the Core Data model.

3. What is a property?
A property is a part of an object which encapsulates its data through properties.
In order to perform tasks, most objects need to track information and store data in properties.Some objects are more general in scope, perhaps handling the interaction between a user interface and the information it displays, but even these objects need to keep track of user interface elements or the related model objects.

4. What’s wrong with this property definition
@property (nonatomic, strong) NSUInteger foobarInteger;

Attribute ‘strong’ requires the property of object type instead of ‘NSUInteger’

5. Why are all UIKit outlets declared nonatomic?
i.e. @property (nonatomic, strong) IBOutlet UIButton *fooButton

It’s not a requirement. In fact you can get away with not declaring properties at all.
However, people use properties because of easier memory management (the retain keyword will ensure the outlet stays in memory until release is called; it would be released when the view was unloaded otherwise I believe). The nonatomic just follows along because it is set to atomic by default — if there is no reason to use atomic you should use nonatomic.

It is NOT the default behavior but faster (for synthesized code, that is, for variables created using @property and @synthesize) and
not thread-safe may result in unexpected behavior, when two different process access the same variable at the same time.

Since user interface elements of both UIKit and Cocoa are only ever intended to be accessed from the main thread the extra lock is unnecessary. The overhead of making a property atomic is pretty minimal, but in the more constrained environment of iOS every little ounce of speed is valuable.. hence why iOS defaults to using nonatomic properties for IB Outlets.

6. Whats wrong with this code?
@interface InstaClass
@property (strong, nonatomic) NSString *someInstacartString;
@end

@implementation InstaClass

– (id)init {
if (self = [super init]) {
self.someInstacartString = @”Something”;
}
return self;
}

– (void)anInstacartMethod {
void (^someBlock)() = ^{
NSString *tempString = self.someInstacartString;
NSLog(@”%@”, tempString);
};
}

Solution:
block will retain the self, so assigning it to weak one will avoid throw the exception.

– (void)anInstacartMethod {
__weak __typeof(self)weakSelf = self;
void (^someBlock)() = ^{
NSString *tempString = weakSelf.someInstacartString;
NSLog(@”%@”, tempString);
};
}

Reference:

http://stackoverflow.com/questions/3424332/are-iboutlet-properties-required-to-be-nonatomic

http://stackoverflow.com/questions/588866/whats-the-difference-between-the-atomic-and-nonatomic-attributes

http://stackoverflow.com/questions/15362157/why-are-cocoas-iboutlet-properties-atomic-by-default-and-cocoa-touchs-arent

 

 

Advertisements

About liyao13

Yao Li is a web and iOS developer, blogger and he has a passion for technology and business. In his blogs, he shares code snippets, tutorials, resources and notes to help people develop their skills. Donate $5 to him for a coffee with PayPal at About Me page and read more professional and interesting technical blog articles. Follow him @Yaoli0615 at Twitter to get latest tech updates.
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s