【问题标题】:Cannot set property, CLLocation无法设置属性,CLLocation
【发布时间】:2014-06-01 11:43:58
【问题描述】:

我正在尝试使用以下代码设置自定义类的 CLLocation 属性(名为 objectLocation),该代码从我的主 ViewController 中调用。不幸的是,我在“注释”行收到一条错误消息,告诉我“表达式不可赋值”。 locationArray 是自定义类的对象数组。我真的需要设置这个值,所以任何帮助表示赞赏!

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
for (int i = 0; i < [locationsArray count]; i++) {

if ([locationsArray[i] objectLocation] == NULL) {
    [locationsArray[i] objectLocation] = [locations lastObject]; //retrieves most recent location data - THIS IS THE LINE THAT RECEIVES THE ERROR
    //now set up a region and start monitoring data for that region
    [locationsArray[i] region] = [[CLRegion alloc]
                                          initCircularRegionWithCenter:
                                          [[locationsArray[i] objectLocation] coordinate]
                                                                radius:2
                                                            identifier:[NSString stringWithFormat:@"%@", [locationsArray[i] objectLocationName]]];
       }
}
}

【问题讨论】:

    标签: ios objective-c cllocationmanager cllocation


    【解决方案1】:

    如果它是您班级中CLLocation 的属性,并且您的locationsArray 持有该对象,而您的locations 数组确实持有CLLocation 对象,请尝试以下操作:

    ((<YOUR_CLASS>*)[locationsArray[i]).objectLocation = [locations lastObject]; 
    

    希望这会有所帮助。

    【讨论】:

    • 铸造工作允许“分配”。但是,CLLocations 仍然应该是空的......你知道如何解决这个问题吗?我想将最近的位置设置为属性值,前提是该值尚未设置。
    • @Jason 哪个CLLocation?您的位置数组中的那个还是位置中的那个?哪个应该是空的?听起来也许您可以显示更多代码?
    • locationsArray 只是保存自定义类的实例。 objectLocation 是自定义类对象的 CLLocation 属性。这是我要设置的 CLLocation。每次应用程序启动时,我都会使用持久数据重新加载数组的值,并 NSLog 数组中每个对象的 objectLocation。不幸的是,它总是记录为空。是否有您想查看的特定代码?我想不出任何其他特别有用的代码。
    • 我已经编辑了代码。我在调用 didUpdateLocation 的开头放置了一个 NSLog,以便我可以检查位置数组的值。我还在自定义类中初始化了我的 objectLocation。现在永远不会调用位置数组的 NSLog,并且 objectLocation 坐标是 (0, 0)。
    猜你喜欢
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    相关资源
    最近更新 更多