0

我知道我不能直接把它们放进去,但我必须把它们转换成一个NSNumber,例如。我将它们转换为NSNumbers,然后将它们放入字典中。我立即记录我正在填充的键,它们是空白的。

这是一个例子:

- (NSDictionary*) dictionaryFromWrestler
{
    /* 
     * Used ot create a dictionary from the instance of the wrestler
     * Can be used to send and receive wrestlers through notification center
     */
    NSDictionary* dictionary;

    NSNumber* _score = [NSNumber numberWithInt:score];


    dictionary = [NSDictionary dictionaryWithObjectsAndKeys:firstName, @"firstName", lastName, @"lastName", fullName, @"fullName", shortName, @"shortName", team, @"team", seed, @"seed", actualWeight, @"actualWeight", dob, @"dob", club, @"club", hometown, @"hometown", state, @"state", grade, @"grade", extraField, @"extraField",  phone, @"phone", address, @"address", zip, @"zip", record, @"record", gradeAbbr, @"gradeAbbr", twid, @"twid", teamId, @"teamId", position, @"position", _score, @"score", [NSNumber numberWithInt:teamScore], @"teamScore", [NSNumber numberWithInt:period1Score], @"period1Score", [NSNumber numberWithInt:period2Score], "@period2Score", [NSNumber numberWithInt:periods], @"periods", [NSNumber numberWithBool:hasRidingTime], @"hasRidingTime", nil];

NSLog(@"dictionaryFromWrestler Score: %@", lastName);
NSLog(@"dictionaryFromWrestler Score: %i", score);
NSLog(@"dictionaryFromWrestler Score: %@", _score);
NSLog(@"dictionaryFromWrestler Score: %d", [[dictionary objectForKey:@"score"] intValue]);
NSLog(@"dictionaryFromWrestler Score: %@", [dictionary valueForKey:@"score"]);
NSLog(@"dictionaryFromWrestler Score: %@", [dictionary valueForKey:@"lastName"]);

    return dictionary;
}

我的日志如下所示:

2012-05-18 17:46:53.117 Wrestling Tools[31509:403] dictionaryFromWrestler Score: Brown
2012-05-18 17:46:53.117 Wrestling Tools[31509:403] dictionaryFromWrestler Score: 2
2012-05-18 17:46:53.117 Wrestling Tools[31509:403] dictionaryFromWrestler Score: 2
2012-05-18 17:46:53.117 Wrestling Tools[31509:403] dictionaryFromWrestler Score: (null)
2012-05-18 17:46:53.117 Wrestling Tools[31509:403] dictionaryFromWrestler Score: (null)
2012-05-18 17:46:53.117 Wrestling Tools[31509:403] dictionaryFromWrestler Score: Brown

如您所见,原语失败了,但对象工作。有没有搞错?

4

2 回答 2

4

"%@" 是对象的格式说明符,但您得到的是数字的 intValue。您想要NSLog(@"dictionaryFromWrestler Score: %d", [[dictionary objectForKey:@"score"] intValue])NSLog(@"dictionaryFromWrestler Score: %@", [dictionary objectForKey:@"score"])

于 2012-05-18T22:26:03.630 回答
2

该方法在遇到值-[NSDictionary dictionaryWithObjectsAndKeys:]时停止处理其参数列表。nil这不仅仅意味着nil你写的文字;它是计算结果为的任何参数nil(因为,当然,该方法无法区分)。

_score因此,在 , 之前的looong 列表中的某个值肯定会nil过早地截断列表。因此,字典确实没有关键的“分数”。

于 2012-05-19T13:36:47.577 回答