2013-07-19 44 views
1

我來自Actionscript Background。在Actionscript中,類方法只能訪問類方法和類屬性。類方法如何訪問實例方法?

但在Objective C,

  1. 如何類方法gameResultAll可以訪問實例方法initFromPlist

    +(NSMutableArray *)gameResultAll://Class Method 
    
    -(id)initFromPlist:(id)plist;//Instance Method 
    
    NSMutableArray *gameResults = [GameResult gameResultAll]; // (returns GameResult array) 
    
  2. 爲什麼[個體經營的init]方法被調用,而不是[超級初始化]創建來自類方法的實例。

在此先感謝。

#import "GameResult.h" 

@implementation GameResult 

#define GAME_RESULT_KEY @"gameresult_key" 
#define SCORE_KEY @"score" 

+(NSMutableArray *)gameResultAll 
{ 

    NSMutableArray *resultArray = [[NSMutableArray alloc] init]; 
    for (id plist in [[[[NSUserDefaults standardUserDefaults] dictionaryForKey:GAME_RESULT_KEY] mutableCopy] allValues]) 
    { 
     GameResult *gameResult = [[GameResult alloc] initFromPlist:plist]; 
     [resultArray addObject:gameResult]; 
    } 
    return resultArray; 
} 

//Designated initialiser 
-(id)initFromPlist:(id)plist 
{ 

    self = [self init]; 
    if(self) 
    { 
     if([plist isKindOfClass:[NSDictionary class]]) 
     { 
      NSDictionary *resultDictionary = (NSDictionary*)plist; 
      _score = (int)resultDictionary[SCORE_KEY]; 
     } 
    } 
    return self; 
} 
+0

@ H2CO3 lololol我只是想說, –

+0

對不起,我第一次使用。我更新了。 – partikles

回答

2

在ActionScript類方法只能訪問類方法和類屬性。

這是Objective-C中沒有什麼不同或者(因爲沒有別的將使意義),所以:

如何類方法GameResultAll只能通過有效的訪問實例方法initFromPlist

實例。

爲什麼要調用[self init]方法而不是[self super]從類方法創建實例。

因爲後者是語法錯誤,也許?閱讀基本的Objective-C教程。

+0

對不起@ H2CO3,更新。 – partikles

+0

@Rob感謝您的理解 – partikles