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

发布于 2024-10-31 07:36:07 字数 330 浏览 2 评论 0原文

我想将“city”的值获取到“CurrentLocation”中。

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

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

I want to get the value of "city" into "CurrentLocation".

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皓月长歌 2024-11-07 07:36:08

为什么您的反向地理编码器消息返回无效?我会这样写:

- (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];
}

Why are your returning void from your reverseGeocoder message? I would have written this like this:

- (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];
}

In this code I am assuming Placemark is a class with a addressDictionary NSDictionary defined as a property.

If you really need that message to return a void* then you would cast from a NSString* to void* and then back again.

- (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;
}

Then cast it back to an NSString when you assign it (not sure why you would do this):

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

    NSString * CurrentLocation = (NSString*)[self reverseGeocoder:myPlacemark];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文