【问题标题】:Compilation error with Xcode 4.2 [closed]Xcode 4.2的编译错误[关闭]
【发布时间】:2011-06-25 04:47:48
【问题描述】:

好的,我想我正式发疯了...尝试使用 iOS 5.0 模拟器在 xcode 4.2 上运行此代码并遇到各种错误。在这个应用程序中,我要做的就是运行核心位置 API。

我的位置“控制器”类:

.h:

#import Foundation/Foundation.h
#import CoreLocation/CoreLocation.h

@protocol locationReceiverDelegate
@required

- (void)locationUpdate:(CLLocation *)location;
- (void)locationError:(NSError *)error;

@end

@interface locationReceiver : NSObject <CLLocationManagerDelegate> {
    CLLocationManager *locMgr;
    id delegate;
}

@property (nonatomic, retain) CLLocationManager *locMgr;
@property (nonatomic, assign) id delegate;

@end

.m:

#import "locationReceiver.h"

@implementation locationReceiver

@synthesize locMgr, delegate;


- (id)init
{
    self = [super init]; 
    if(self != nil) {
        self.locMgr = [[CLLocationManager alloc] init];
        self.locMgr.delegate = self;
    }

    return self; 
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation     *)newLocation fromLocation:(CLLocation *)oldLocation {
//  if([self.delegate conformsToProtocol:@protocol(locationReceiverDelegate)]) {
        [self.delegate locationUpdate:newLocation];
//      }
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
//  if([self.delegate conformsToProtocol:@protocol(locationReceiverDelegate)]) {
        [self.delegate locationError:error];
//  }
}


@end

我在“@synthesize locMgr, delegate;”的 .m 文件中遇到错误即“分配属性 'delegate' 的现有 ivar 'delegate' 必须是 _unsafe_unretained”

【问题讨论】:

  • 这可能不是您的问题,但 ivar 和属性之间存在类型不匹配:ivar 是 id,但属性是 id&lt;locationReceiverDelegate&gt;
  • 在 Apple 开发者论坛上提问,Xcode 4.2 尚未发布,我们无法在这里回答 ARC 问题。

标签: iphone objective-c compiler-construction xcode4 location


【解决方案1】:

你不应该assign你的代表,而是应该这样做

 @property (nonatomic, retain) id delegate;

这应该可以解决您的问题。

【讨论】:

  • 太棒了,谢谢 - 确实做到了!现在我根本没有得到位置:(....
  • 好吧,你还记得将自己设置为初始化它的类中的“控制器”类的委托吗?
  • 点赞myController.delegate = self
  • 这是我的控制器文件 (locationReceiver.h): #import #import @protocol locationReceiverDelegate @required - (void)locationUpdate:(CLLocation * )地点; - (void)locationError:(NSError *)error; @end @interface locationReceiver : NSObject { CLLocationManager *locMgr;身份代表; @property (nonatomic, 保留) CLLocationManager *locMgr; @property(非原子,保留)id 委托; @end
  • 和 .m 文件:#import "locationReceiver.h" @implementation locationReceiver @synthesize locMgr; @synthesize 代表; - (id)init { self = [超级初始化]; if(self != nil) { self.locMgr = [[CLLocationManager alloc] init]; self.locMgr.delegate = self; } 返回自我; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { } @end
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多