1

Q)is it a requirement that all interface classes inherit from super class? Q) In the code below - from iOS5 on wards with ARC do i still need to do (player is NSMUtable array) unless i'm wrong i believe after ARC we dont need to keep ref count:

//is this required then? Player *player = [[Player alloc] init];

{players = [NSMutableArray arrayWithCapacity:20];
Player *player = [[Player alloc] init];
player.name = @"Bill Evans";
player.game = @"Tic-Tac-Toe";
player.rating = 4;
[players addObject:player];
player = [[Player alloc] init];
player.name = @"Oscar Peterson";
player.game = @"Spin the Bottle";
player.rating = 5;
[players addObject:player];
player = [[Player alloc] init];
player.name = @"Dave Brubeck";
player.game = @"Texas Hold’em Poker";
player.rating = 2;
[players addObject:player];}

Q) what is the rule around making interface file and implementation file sub class of i.e. in java everything is derived from OBject class so do we declare NSObject for both implementaiotn / interface classes or its not needed and where else i would define this?

Q) which one is recomended solution A: Player *player = [[Player alloc] init]; OR B: Player *player = [[Player new]; //unless this is illegal?

Q) is ARC only available on Mac OS X apps or also on iOS i.e. iphone / touch etc or we still have to do manual memory management?

Q) can singleton pattern be applied to Objective-c,cocoa touch iphone apps?

ipmcc
  • 29,581
  • 5
  • 84
  • 147
codejunkie
  • 1,122
  • 3
  • 13
  • 29

1 Answers1

1
  • you have to do [[Player alloc] init] to instantiate the object
  • always derive your classes from NSObject, unless you want to specifically derive from some other class
  • [[Player alloc] init] is equivalent to [Player new], but the former is preferred, since you can easily swap for another initializer, such as [[Player alloc] initWithName:@"John Doe"]
  • ARC is available both on iOS and on Mac, where it should be used instead of garbage collection, which is somewhat being deprecated
  • I don't see why you wouldn't be able to create singletons on iOS
Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
  • >> always derive your classes from NSObject, unless you want to specifically derive from some other class -i understand that but i guess what i'm trying to ask is do we define it explicitly or compiler takes care of it? -as for singleton pattern i couldnt find credible code of iOS5 on this any ideas? looking at > [[Player alloc] initWithName:@"John Doe"] i'm bit lost here is initWithName is a method on NSMuttable array class. – codejunkie Jan 13 '12 at 18:46
  • @RedMan NSObject is a basic class for Cocoa, but you're not required to derive from it. If you don't do it explicitly, you won't get the functionality it provides (https://developer.apple.com/library/prerelease/ios/#documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html%23//apple_ref/occ/intf/NSObject). As for the singleton, I don't see why it should be different than in non-iOS environment, see this question for example http://stackoverflow.com/questions/145154/what-does-your-objective-c-singleton-look-like – Jakub Arnold Jan 13 '12 at 18:54
  • Just add for others reference Class method is ideal and only way to use singleton pattern. – codejunkie Jan 13 '12 at 19:05
  • The "only way"? That's a bit extreme. It is the most common pattern, yes. For reference, I described one common way to implement that pattern over here: http://stackoverflow.com/questions/8671784/how-to-get-the-address-of-an-objective-c-object-into-a-void-volatile-under-a/8688595#8688595 As for the other issue, you would want have a very specific reason to make a new root class (i.e. to not transitively inherit an Obj-C class from NSObject or NSProxy.) It's not a practice there are a whole lot of strong cases for. – ipmcc Jan 14 '12 at 15:17
  • root class? - isnt NSObject and NSProxy would be root at super class so no matter what you cant replace root class.. now im bit confused if you can explain when you say root class – codejunkie Jan 16 '12 at 09:40