【问题标题】:ARC semantic Issue, Semantic issue error.ARC语义问题,语义问题错误。
【发布时间】:2014-03-19 03:31:22
【问题描述】:

我已经下载了这个Raywenderlich tutorial,但我对这部分代码有疑问。

我还是 iOS 开发的新手,我不明白这意味着什么。我在互联网上搜索过,但没有一个是答案。

请帮帮我。

int index = [_locations indexOfObjectPassingTest:^(id obj, NSUInteger index, BOOL *stop) {
    return [[obj location] isEqual:location];
}]; 

有3个错误如下:

发现多个名为“location”的方法的结果、参数类型或属性不匹配

不兼容的块指针类型将'void (^)(__strong id, NSUInteger, BOOL *)'发送到'BOOL (^)(__strong id, NSUInteger, BOOL *)'类型的参数

错误的接收器类型“CGFloat”(又名“float”)

这是完整的代码,您可以在其中找到这一行:

- (void)didTouchMarkerView:(MarkerView *)markerView 
{
    ARGeoCoordinate *tappedCoordinate = [markerView coordinate];
    CLLocation *location = [tappedCoordinate geoLocation];

    int index = [_locations indexOfObjectPassingTest:^(id obj, NSUInteger index, BOOL *stop) {
    return [[obj location] isEqual:location];
    }]; 

    if(index != NSNotFound) 
    {
        Place *tappedPlace = [_locations objectAtIndex:index];
        [[PlacesLoader sharedInstance] loadDetailInformation:tappedPlace successHanlder:^(NSDictionary *response) {
            NSLog(@"Response: %@", response);
            NSDictionary *resultDict = [response objectForKey:@"result"];
            [tappedPlace setPhoneNumber:[resultDict objectForKey:kPhoneKey]];
            [tappedPlace setWebsite:[resultDict objectForKey:kWebsiteKey]];
            [self showInfoViewForPlace:tappedPlace];
     } errorHandler:^(NSError *error) {
        NSLog(@"Error: %@", error);}];
    }
}

【问题讨论】:

    标签: ios iphone location augmented-reality semantics


    【解决方案1】:

    只需将return语句更改如下,

    return [[(Place *)obj location] isEqual:location];
    

    它会消除错误。

    【讨论】:

    • 遇到了同样的问题。此答案应标记为该特定问题的已接受答案。
    【解决方案2】:

    正如所写,您正在传递一个返回 void 的块,但方法签名要求您传入一个返回 BOOL 的块。

    只需在您作为参数传入的块中添加一点:

    int index = [_locations indexOfObjectPassingTest:^BOOL(id obj, NSUInteger index, BOOL *stop) {
    

    以后,当代码完成提示块签名时,只需按 ENTER 或 TAB,您就不必担心记住语法...

    至于找到的多个名为location的方法,你有:

    1. 一个名为locations 的属性,您通过合成的getter 作为_locations 访问它
    2. 一个名为 locationCLLocation 实例,其作用域是方法
    3. 您还向程序做出了强有力的承诺,即存储在您的_locations 属性中的每个对象都绝对有一个属性或它自己的称为location 的方法,它可能等于也可能不等于作用域的方法location 来自#2

    我是否可以建议您尝试一种“清洁代码”人士青睐的策略,并沉迷于更长、更具描述性的方法和变量名称。简单地这样做可能会突出您的location 不匹配,并让您了解发生了什么。

    很难从发布的代码量推测 CGFloat 错误接收器类型,但我敢打赌,您会发生一些 NSNumber / CGFloat 不匹配的情况。你可以使用[NSNumber numberWithFloat:someFloat]创建一个NSNumber,你可以调用[someNumber floatValue]从一个NSNumber中提取一个浮点数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多