如何从“(void)”方法将NSString值转换为Objective-C中的“(BOOL)”方法?

时间:2011-04-07 01:55:38

标签: objective-c variables nsstring

我想将“city”的值变为“CurrentLocation”。

- (void)reverseGeocoder:(MKPlacemark *)placemark {
    NSString *city = [myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressCityKey];
}

- (BOOL)fetchAndParseRSS {        
    NSString *currentLocation; // I want 'city' here.
    return YES;
}

1 个答案:

答案 0 :(得分:3)

为什么你的reverseGeocoder消息会返回空格?我会写这样的:

- (NSString*)reverseGeocoder:(Placemark*)myPlacemark
{
    // assuming myPlacemark is holding a reference to the dictionary (so no need to retain)
    NSString *city = [myPlacemark.addressDictionary objectForKey:kABPersonAddressCityKey];
    return city;
}

-(BOOL)fetchAndParseRss
{
    // you need to get myPlacemark from somewhere, presumably from the geocode request?
    Placemark * myPlacemark = [self getPlacemark];

    NSString * CurrentLocation = [self reverseGeocoder:myPlacemark];
}

在这段代码中,我假设Placemark是一个将addressDictionary NSDictionary定义为属性的类。

如果你确实需要该消息来返回一个void *那么你会从NSString *转换为void *然后再返回。

- (void*)reverseGeocoder:(Placemark*)myPlacemark
{
    // assuming myPlacemark is holding a reference to the dictionary (so no need to retain)
    NSString *city = [myPlacemark.addressDictionary objectForKey:kABPersonAddressCityKey];
    return (void*)city;
}

然后在分配时将其强制转换为NSString(不知道为什么要这样做):

-(BOOL)fetchAndParseRss
{
    // you need to get myPlacemark from somewhere, presumably from the geocode request?
    Placemark * myPlacemark = [self getPlacemark];

    NSString * CurrentLocation = (NSString*)[self reverseGeocoder:myPlacemark];
}