2013-10-04 3 views
0

예전에 여기 getBoardingCards를 호출 오전 : 배열 비록 null을 반환배열이 null을 반환하는 이유는 무엇입니까?

BRPBoardingCards *boardingCards = [[BRPBoardingCards alloc] init]; 
boardingCardsArray = [boardingCards getBoardingCards]; 

합니다 (NSLog 참조). 내가 도대체 ​​뭘 잘못하고있는 겁니까?

- (NSArray*)getBoardingCards 
{ 
    NSMutableArray *storedArray = [[NSMutableArray alloc] init]; 
    Utils *utils = [[Utils alloc] init]; 
    NSUserDefaults *properties = [utils getUserDefaults]; 



    // check if its the first time we are running the app. 
    if(![[properties objectForKey:@"first_run"] isEqualToString:@"no"]){ 
     //I decided it is for american tourists and every booking office allows payment in dollars because I dont have a euros key on my keyboard. 
     //Car also has lots of seats. 1337 of them to be precise. 
     [self setNewBoardingCardWithCardType:@"car" WithPrice:@"$1.50" AndStartLocation:@"airport" AndEndLocation:@"restaurant" WithSeat:@"1337" andExtraInformation:@"We dont like you so we are giving you the back seat by yourself."]; 
     [self setNewBoardingCardWithCardType:@"helicopter" WithPrice:@"$500" AndStartLocation:@"restaurant" AndEndLocation:@"hospital" WithSeat:@"1" andExtraInformation:@"You are driving."]; 
     [self setNewBoardingCardWithCardType:@"train" WithPrice:@"$100" AndStartLocation:@"restaurant" AndEndLocation:@"ditch" WithSeat:@"STANDONHEAD" andExtraInformation:@"No Seats on this train. Gotta stand on your head. Dont forget your white lightning for the ditch."]; 
     [self setNewBoardingCardWithCardType:@"Fire Engine" WithPrice:@"$10" AndStartLocation:@"restaurant" AndEndLocation:@"burning house" WithSeat:@"HOSE" andExtraInformation:@"Take the hose to put out this fire we are heading to. "]; 
     [self setNewBoardingCardWithCardType:@"Nimbus" WithPrice:@"$300" AndStartLocation:@"burning house" AndEndLocation:@"popo floating island" WithSeat:@"LEVITATION" andExtraInformation:@"Dont forget to turn super saiyan on your way up there. "]; 
     [self setNewBoardingCardWithCardType:@"Sky Dive" WithPrice:@"$1020" AndStartLocation:@"popo floating island" AndEndLocation:@"home" WithSeat:@"GRAVITY" andExtraInformation:@"Ohh! that Adrenalines pumping. "]; 

     [self setNewBoardingCardWithCardType:@"Legs" WithPrice:@"$50" AndStartLocation:@"ditch" AndEndLocation:@"park bench hotel" WithSeat:@"VODKA" andExtraInformation:@"Time to get a good nights rest and sobre up. "]; 
     [self setNewBoardingCardWithCardType:@"Bicycle" WithPrice:@"$5" AndStartLocation:@"park bench hotel" AndEndLocation:@"home" WithSeat:@"BIKESEAT1" andExtraInformation:@"What a terrible hangover. Time to head home. "]; 

     [self setNewBoardingCardWithCardType:@"ambulance" WithPrice:@"$40" AndStartLocation:@"hospital" AndEndLocation:@"home" WithSeat:@"LEVITATION" andExtraInformation:@"Well that was a bad idea! "]; 

     [properties setObject:@"no" forKey:@"first_run"]; 
     [properties synchronize]; 

     NSLog(@"did set first run to no"); 
    } 

     storedArray = [[properties objectForKey:@"cards"] mutableCopy]; 


     NSLog(@"getBoardingCards storedArray: %@", storedArray); 

    return storedArray; 
} 

- (NSArray*)getBoardingCardsByType:(NSString*)cardType 
{ 
    // at this point i would create a for loop and iterate through the array checking if (type isEqualToString:cardType). 
    // But this is just an idea for later. To go by only a certain type of route. 
    return nil; 
} 



- (void)setNewBoardingCardWithCardType:(NSString*)cardType WithPrice:(NSString*)price AndStartLocation:(NSString*)startLocation AndEndLocation:(NSString*)endLocation WithSeat:(NSString*)seatCode andExtraInformation:(NSString*)extraInfo{ 

    // populate the dictionary with data. 
    NSMutableDictionary *card = [[NSMutableDictionary alloc] init]; 
    [card setObject:cardType forKey:@"type"]; 
    [card setObject:price forKey:@"price"]; 
    [card setObject:startLocation forKey:@"startLocation"]; 
    [card setObject:endLocation forKey:@"endLocation"]; 
    [card setObject:seatCode forKey:@"seat"]; 
    [card setObject:extraInfo forKey:@"info"]; 

    // get our stored array. 
    Utils *utils = [[Utils alloc] init]; 
    NSUserDefaults *properties = [utils getUserDefaults]; 
    NSMutableArray *storedArray = [[properties objectForKey:@"cards"] mutableCopy]; 

    [storedArray addObject:card]; 
    // set the stored array. 
    [properties setObject:storedArray forKey:@"cards"]; 
    [properties synchronize]; 

} 
+1

'storedArray = [[ "cards"] mutableCopy];'에서 무조건적으로 재정의하면'storedArray = [[NSMutableArray alloc] init] '설정의 의미는 무엇입니까? – dasblinkenlight

+0

배열에 nil 항목을 포함 할 수 없습니다. 배열이 "null을 반환"하면 배열이 존재하지 않는다는 것이 거의 확실합니다. –

+0

여기서 'cards'키의 값을 설정합니까? – manujmv

답변

4

당신은 NSUserDefaults에서 cards 가변 배열을 만드는 아닙니다. (setNewBoardingCardWithCardType:에서) 이 줄 :

NSMutableArray *storedArray = [[properties objectForKey:@"cards"] mutableCopy]; 

은 기본적으로이 키 오브젝트가 존재하지 않을 경우 경우

NSMutableArray *storedArray = nil; 

확인과 동일

NSMutableArray *storedArray = [nil mutableCopy]; 

이다 -을 만들 사용하십시오.

+0

감사했습니다! 나는 몰라 어떻게 나를 바보처럼 그리워! – jimbob

+1

항상 발생합니다. – Alexander

관련 문제