Jump to content

The guest who is teaching Objective-C is teaching methods I don 't exactly agree with


G+_James Coleman
 Share

Recommended Posts

The guest who is teaching Objective-C is teaching methods I don't exactly agree with.

 

Using macros to store keys.

Using global variables to store notes.

Using NSUserDefaults to store data for the app.

 

The issue with macros is all they do is tell the compiler, wherever you see this variable, replace with what's here. So basically, every time you use the key, you make another copy of the key in binary. What I would use instead of macros is a constant variable:

NSString * const kMyKey =@"Key";

 

The issue with using global variables is that they are not contained within a class and the only reason you would ever want to use a global variable is if you need the data to stay across all new instances of a class. Or if you want to make a class so that it can have one instance that can be retrieved via a class method. This is called a singleton http://www.idev101.com/code/Objective-C/singletons.html

 

The issue with using NSUserDefaults to store persistent data is that it's made for preferences and not data and you have to do what the guest did making it an NSData object. What I suggest doing for a notes app is just using the NSArray's built in method writeToFile https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/index.html#//apple_ref/occ/instm/NSArray/writeToFile%3Aatomically%3A using this will allow you to store to an file within the file system provided to your app. On an iOS device, that would be the Documents folder provided which can be found via [@"~/Documents/" stringByExpandingTildeInPath]. You can place the name of the file after the last / to get the path for the file under the documents folder ready to be written to.

 

I am a long time Objective-C guy, been using it for years now since Objective-C 1.0 and still write in 1.0 syntax.

Link to comment
Share on other sites

 Share

×
×
  • Create New...