问题描述
请帮助!我的第一个程序在目标c。跟随一个教学词的字,但它给我这个错误,我不知道如何阅读目标c。
Help please! my first program in objective c. Followed a tutorial word for word but it gives me this error that I don't know quite how to read for objective c.
SimpleCar.h:
SimpleCar.h:
#import <Cocoa/Cocoa.h>
@interface SimpleCar : NSObject {
NSString* make;
NSString* model;
NSNumber* vin;
}
// set methods
- (void) setVin: (NSNumber*)newVin;
- (void) setMake: (NSString*)newMake;
- (void) setModel: (NSString*)newModel;
// convenience method
- (void) setMake: (NSString*)newMake
andModel: (NSString*)newModel;
// get methods
- (NSString*) make;
- (NSString*) model;
- (NSNumber*) vin;
@end
SimpleCar.m:
SimpleCar.m:
#import "SimpleCar.h"
@implementation SimpleCar
// set methods
- (void) setVin: (NSNumber*)newVin {
[vin release];
vin = [[NSNumber alloc] init];
vin = newVin;
}
- (void) setMake: (NSString*)newMake {
[make release];
make = [[NSString alloc] initWithString:newMake];
}
- (void) setModel: (NSString*)newModel {
[model release];
model = [[NSString alloc] initWithString:newModel];
}
// convenience method
- (void) setMake: (NSString*)newMake
andModel: (NSString*)newModel {
// Reuse our methods from earlier
[self setMake:newMake];
[self setModel:newModel];
}
- (NSString*) make {
return make;
}
- (NSString*) model {
return model;
}
- (NSNumber*) vin {
return vin;
}
-(void) dealloc
{
[vin release];
[make release];
[model release];
[super dealloc];
}
@end
CarApp.m: p>
CarApp.m:
#import <Foundation/Foundation.h>
#import "SimpleCar.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
SimpleCar *myCar = [[SimpleCar alloc] init];
NSNumber *newVin = [NSNumber numberWithInt:123];
[myCar setVin:newVin];
[myCar setMake:@"Honda" andModel:@"Civic"];
NSLog(@"The car is: %@ %@", [myCar make], [myCar model]);
NSLog(@"The vin is: %@", [myCar vin]);
[myCar release];
[pool drain];
return 0;
}
编译器调用:
bash-3.2$ gcc CarApp.m SimpleCar.m -g -m64
错误:
Undefined symbols for architecture x86_64:
"___CFConstantStringClassReference", referenced from:
CFString in ccR0Zlgm.o
CFString in ccR0Zlgm.o
CFString in ccR0Zlgm.o
CFString in ccR0Zlgm.o
"_objc_msgSend", referenced from:
_main in ccR0Zlgm.o
-[SimpleCar setVin:] in ccJfVliU.o
-[SimpleCar setMake:] in ccJfVliU.o
-[SimpleCar setModel:] in ccJfVliU.o
-[SimpleCar setMake:andModel:] in ccJfVliU.o
(maybe you meant: l_objc_msgSend_fixup_release, l_objc_msgSend_fixup_alloc )
"_NSLog", referenced from:
_main in ccR0Zlgm.o
"_objc_msgSend_fixup", referenced from:
l_objc_msgSend_fixup_alloc in ccR0Zlgm.o
l_objc_msgSend_fixup_release in ccR0Zlgm.o
l_objc_msgSend_fixup_release in ccJfVliU.o
l_objc_msgSend_fixup_alloc in ccJfVliU.o
(maybe you meant: l_objc_msgSend_fixup_release, l_objc_msgSend_fixup_alloc )
"_OBJC_CLASS_$_NSAutoreleasePool", referenced from:
objc-class-ref in ccR0Zlgm.o
"_OBJC_CLASS_$_NSNumber", referenced from:
objc-class-ref in ccR0Zlgm.o
objc-class-ref in ccJfVliU.o
"_objc_msgSendSuper2", referenced from:
-[SimpleCar dealloc] in ccJfVliU.o
"_OBJC_METACLASS_$_NSObject", referenced from:
_OBJC_METACLASS_$_SimpleCar in ccJfVliU.o
"__objc_empty_cache", referenced from:
_OBJC_METACLASS_$_SimpleCar in ccJfVliU.o
_OBJC_CLASS_$_SimpleCar in ccJfVliU.o
"__objc_empty_vtable", referenced from:
_OBJC_METACLASS_$_SimpleCar in ccJfVliU.o
_OBJC_CLASS_$_SimpleCar in ccJfVliU.o
"_OBJC_CLASS_$_NSObject", referenced from:
_OBJC_CLASS_$_SimpleCar in ccJfVliU.o
"_OBJC_CLASS_$_NSString", referenced from:
objc-class-ref in ccJfVliU.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
推荐答案
看起来像你刚刚创建一个没有Xcode项目的Xcode工作区。您可以使用以下项目:
It looks like you just created an Xcode Workspace without an Xcode project. Here's a project that you could use:
通常,您只需选择文件>新建项目即可创建新项目。
Generally, you just choose File > New Project to create a new project. You'd likely want a Foundation-based command-line program for this particular project.
不幸的是,教程没有给出最好的实现 SimpleCar
类。下面是一个可能的重写:
Unfortunately, the tutorial didn't give the best implementation of the SimpleCar
class. Below is one possible rewrite:
#import "SimpleCar.h"
@implementation SimpleCar
-(void) dealloc
{
[vin release];
[make release];
[model release];
[super dealloc];
}
// set methods
- (void) setVin: (NSNumber*)newVin {
[newVin retain];
[vin release];
vin = newVin;
// [vin release];
// vin = [[NSNumber alloc] init];
// vin = newVin;
}
上面注释的代码是原始代码。它既具有潜在的危险,也泄漏记忆。原始代码的第一行在执行任何其他操作之前释放 vin
,这是潜在的危险。例如,如果您在 CarApp.m
中执行了此操作:
The commented code above is the original code. It is both potentially dangerous and also leaks memory. The first line of the original code releases vin
before doing anything else, which is potentially dangerous. For example, what if in your CarApp.m
you did this:
NSNumber *vinValue = [car vin];
[car setVin:vin];
NSLog(@"car's vin == %@", [car vin]); // unpredictable results/crash
原始代码将释放现有的 vin
值,而不必麻烦,以确保传递的值不是实际上 vin
本身。通过设置 vin = newVin
,将 vin
设置为指向自身,但在释放后。任何随后尝试向 vin
发送邮件将导致崩溃或不可预测的结果。
The original code would have released the existing vin
value without bothering to make sure the value being passed in wasn't actually vin
itself. By setting vin = newVin
, it was setting vin
to point to itself but after being released. Any subsequent attempts to send messages to vin
would result in a crash or unpredictable results.
原始代码也泄漏记忆。 NSNumber
的实例是不可变的,所以通过 alloc / init
创建一个数字并不真正有意义将始终为零,并且永远不能更改)。我的替换代码首先保留传递的值,以防它发生在 vin
。然后释放 vin
,然后将 vin
分配给 newVin
。
The original code also leaks memory. Instances of NSNumber
are immutable, so creating a number through alloc/init
doesn't really make much sense (since it will always be zero, and can never be changed). My replacement code first retains the value that is passed in, in case it happens to be vin
. It then releases vin
and then assigns vin
to newVin
.
setMake:
和 setModel:
方法有问题因为相同的原因。
The setMake:
and setModel:
methods are problematic for the same reasons.
- (void) setMake: (NSString*)newMake {
[newMake retain];
[make release];
make = newMake;
// [make release];
// make = [[NSString alloc] initWithString:newMake];
}
- (void) setModel: (NSString*)newModel {
[newModel retain];
[model release];
model = newModel;
// [model release];
// model = [[NSString alloc] initWithString:newModel];
}
// convenience method
- (void) setMake: (NSString*)newMake
andModel: (NSString*)newModel {
// Reuse our methods from earlier
[self setMake:newMake];
[self setModel:newModel];
}
- (NSString*) make {
return make;
}
- (NSString*) model {
return model;
}
- (NSNumber*) vin {
return vin;
}
@end
这篇关于目标c未定义符号编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!