猿问

Objective-C composite object 合成对象问题

最近看《Objective-C程序设计》,11章有个合成对象
自己写了一个例子Xcode没报错,编译时报错了
//"Rectangle.h"
@interfaceRectangle:NSObject
@propertyintwidth,height;
-(void)setWidth:(int)wandHeight:(int)h;
-(int)area;
@end
#import"Rectangle.h"
@interfaceSquare_c:NSObject
{
Rectangle*rect;
}
-(instancetype)init;
-(instancetype)initWithSide:(int)s;
-(void)setSide:(int)s;
-(int)side;
-(int)area;
@end
@implementationSquare_c
-(instancetype)init{
self=[superinit];
if(self){
rect=[[Rectanglealloc]init];
}
return[selfinitWithSide:0];
}
-(instancetype)initWithSide:(int)s
{
self=[superinit];
if(self)
[rectsetWidth:sandHeight:s];
returnself;
}
-(void)setSide:(int)s
{
[rectsetWidth:sandHeight:s];
}
-(int)side
{
returnrect.width;
}
-(int)area
{
return[rectarea];
}
@end
intmain(intargc,char*argv[])
{
@autoreleasepool{
Square_c*square_c=[[Square_calloc]initWithSide:20];
}
}
Xcode编译后报错
Ld/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Products/Debug/prog1normalx86_64
cd/Project/prog1
exportMACOSX_DEPLOYMENT_TARGET=10.10
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang-archx86_64-isysroot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk-L/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Products/Debug-F/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Products/Debug-filelist/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/prog1.LinkFileList-mmacosx-version-min=10.10-fobjc-arc-fobjc-link-runtime-Xlinker-dependency_info-Xlinker/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/prog1_dependency_info.dat-o/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Products/Debug/prog1
duplicatesymbol_OBJC_IVAR_$_Square2.rectin:
/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/main.o
/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/Square_composite.o
duplicatesymbol_OBJC_CLASS_$_Square2in:
/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/main.o
/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/Square_composite.o
duplicatesymbol_OBJC_METACLASS_$_Square2in:
/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/main.o
/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/Square_composite.o
ld:3duplicatesymbolsforarchitecturex86_64
clang:error:linkercommandfailedwithexitcode1(use-vtoseeinvocation)
求熟手帮忙看下哪里出问题了
噜噜哒
浏览 410回答 2
2回答

慕雪6442864

解决了,把composite的那个.m文件里的@interface拆分出.hmain.m里再引入这个.h就可以成功build了。但是类的分类文件只用.m不分出.h是可以build的

侃侃无极

error:linkercommandfailedwithexitcode1(use-vtoseeinvocation)这个错误信息的意思就是link出错了,如果你不详细的出错信息贴出来的话,那么它并没有什么卵用。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答