我是Objective C的新手,在Windows上使用GNUstep。当我尝试编译以下代码时:
#import <Foundation/Foundation.h>
#import "song.h"
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Set up variables
//Read initial songs
NSError *error;
NSURL *playlistURL = [NSURL fileURLWithPath:@"C:\\ObjCPrograms\\PlayList\\playlist.txt"];
NSString *savedSongs = @"";
savedSongs = [[NSString alloc] initWithContentsOfURL:playlistURL encoding:NSUTF8StringEncoding error:&error];
//Separate initial songs into array
NSMutableArray *savedSongsArray;
NSMutableArray *songLine = (NSMutableArray *)[savedSongs componentsSeparatedByString:@"\n"];
int line = 0;
for(line; line < [songLine count]; line++)
{
NSMutableArray *lineContents = (NSMutableArray *)[[songLine objectAtIndex:line] componentsSeparatedByString:@"|"];
song *newSong = [[song alloc]init];
[song autorelease];
NSString *title = [lineContents objectAtIndex:0];
NSString *performer = [lineContents objectAtIndex:1];
NSString *genre = [lineContents objectAtIndex:2];
NSString *length = [lineContents objectAtIndex:3];
NSLog(@"%@|%@|%@|%@", title, performer, genre, length);
[newSong setTitle: title];
NSLog(@"here");
[newSong setPerformer: performer];
[newSong setGenre: genre];
[newSong setLength: length];
[savedSongsArray addObject:newSong];
}
使用命令
gcc -o PlayList playList.m -I/c/GNUstep/GNUstep/System/Library/Headers \
-L /c/GNUstep/GNUstep/System/Library/Libraries -lobjc -lgnustep-base \
-fconstant-string-class=NSConstantString
我收到错误消息
playList.m:(.data+0x230): undefined reference to `__objc_class_name_song'
collect2: ld returned 1 exit status
我相信我的问题是newsong的声明,因为当我用
替换它时song *newSong;
我的代码编译。但是,当我运行此代码时,程序在我调用newSong的setter时崩溃了:
[newSong setTitle: title];
我检查了,甚至从未调用过setter。根据我的阅读,链接可能存在问题,但我找不到任何拼写错误或丢失代码。
这是我在界面和歌曲实现中的setter方法:
- (void) setTitle: (NSString *) newTitle;
- (void) setTitle: (NSString *) newTitle;
{
[newTitle retain];
[title release];
title = newTitle;
}