【问题标题】:Initializing custom init gives "incompatible type for argument 1 of 'initWithCoord:'"初始化自定义初始化会给出“'initWithCoord:' 的参数 1 的不兼容类型”
【发布时间】:2011-07-07 21:18:58
【问题描述】:

我使用自定义 initWithCoord 创建自己的类,该类以一个 (struct CLLocationCoordinate2D*) 作为参数。

即使我认为我有正确的参数类型,我仍然得到“'initWithCoord: 的参数 1 的不兼容类型:”我做错了什么:

PeopleStub.m:

#import "PeopleStub.h"


@implementation PeopleStub


-(id)initWithCoord:(struct CLLocationCoordinate2D*)coord{
    if(self = [super init]){
            people = [[NSMutableDictionary alloc] init];

    }

    return self; 

}

@end

PeopleStub.h

#import <Foundation/Foundation.h>


@interface PeopleStub : NSObject {

    NSMutableDictionary *people;

}

-(id)initWithCoord:(struct CLLocationCoordinate2D*)coord;

@end

我创建 PeopleStub 实例的方式:

PeopleStub *peopleStub = [[PeopleStub alloc] initWithCoord:locationManager.location.coordinate;

提前致谢!

【问题讨论】:

    标签: objective-c struct initialization location parameter-passing


    【解决方案1】:

    您可能只想传递CLLocationCooridnate2D,而不是指向与参数相同的指针。所以:

    -(id)initWithCoord:(CLLocationCoordinate2D)coord;
    

    【讨论】:

    • 谢谢!当我这样做时,我得到:“'CLLocationCoordinate2D'之前的“预期')'”和“'--(id)initWithCoord:(CLLocationCoordinate2D)coord'的先前声明”。为什么我会这样?感觉像是在引用另一个类似的声明。
    • @Nicsoft,听起来您需要更新声明 (.h) 和定义 (.m) 文件,以便它们匹配签名。顺便说一句,这基本上是 C 的东西。从长远来看,获得一本关于 C 的好书可能会让这一切变得更容易。
    • 我已经做到了。警告“'--(id)initWithCoord:(CLLocationCoordinate2D)coord' 的先前声明”消失,但在 'CLLocationCoordinate2D' 之前的错误“预期 ')' 消失。
    • @Nicsoft,哪一行(.h 或 .m)出现错误?您可以编辑您的答案以显示确切的当前代码吗?如果我们能看到一些代码,帮助会容易得多。
    猜你喜欢
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多