0

我已经自学编程大约 4 个月了。我使用这个网站和另一个论坛来帮助自己解决问题,一个朋友给了我他在学校的旧书。只是为了让你知道为什么我可能会错过一个明显的错误。

我正在开发一个列出歌曲和艺术家的程序。我希望对它们进行排序,然后按艺术家升序显示它们。我觉得我快到了,但我错过了一些东西。

结果将显示但不按顺序显示。我想我需要添加一些说明以升序打印或列出下一首歌曲的内容,但我不确定是什么。这是我的整个代码,感谢您的帮助:

歌曲界面

#import <Foundation/Foundation.h>

@interface Song : NSObject

-(void) setTitle:(NSString *) songTitle;
-(void) setArtist:(NSString *) songArtist;
-(NSString *) Title;
-(NSString *) Artist;

@end

歌曲实现

 #import "Song.h"

@implementation Song

{
    NSString *Title;
    NSString *Artist;
}

-(void) setTitle:(NSString *)songTitle;

{
    Title = [NSString stringWithString: songTitle];
}

-(void) setArtist:(NSString *)songArtist;

{
    Artist = [NSString stringWithString:songArtist];
}

-(NSString *) Title

{
    return Title;
}

-(NSString *) Artist

{
    return Artist;
}

-(NSComparisonResult) compareArtist: (id) element

{
    return [Artist compare: [element Artist]];
}

@end

图书馆界面

#import <Foundation/Foundation.h>
#import "Song.h"

@interface Library : NSObject

@property (nonatomic, strong) NSMutableArray *list;

-(id) initList;

-(void) addSong: (Song *) song;

-(void) listSongs;

-(void) sort;

@end

库实现

    #import "Library.h"

@implementation Library

@synthesize list;

-(id) initList
{
    self = [super init];
    if (self){
        list = [NSMutableArray array];
}
    return self;
}

-(id) init
{
    return [self initList];
}


-(void) addSong:(Song *)song

{
    [list addObject: song];
}



-(void) sort

{
    [list sortUsingSelector: @selector(Artist:)];
}

-(void) listSongs

{
    NSLog(@"The list of my songs");

    for (Song *song in list)
        NSLog(@"%-20s by %s", [song.Title UTF8String], [song.Artist UTF8String]);
}


@end

主要的

#import <Foundation/Foundation.h>
#import "Song.h"
#import "Library.h"

int main(int argc, const char * argv[])

{

    @autoreleasepool {


         // insert code here...
        Song *sonlis1 = [[Song alloc] init];
        Song *sonlis2 = [[Song alloc] init];
        Song *sonlis3 = [[Song alloc] init];
        Song *sonlis4 = [[Song alloc] init];
        Song *sonlis5 = [[Song alloc] init];
        Song *sonlis6 = [[Song alloc] init];
        Song *sonlis7 = [[Song alloc] init];
        Song *sonlis8 = [[Song alloc] init];
        Song *sonlis9 = [[Song alloc] init];
         Song *sonlis10 = [[Song alloc] init];

        Library *myList = [[Library alloc] initList];

        //song 1
        [sonlis1 setTitle: @"Holiday"];
        [sonlis1 setArtist: @"Madonna"];

        //song 2
        [sonlis2 setTitle: @"One"];
        [sonlis2 setArtist: @"Metalica"];


        //song  3
        [sonlis3 setTitle: @"girlfriend"];
        [sonlis3 setArtist: @"Nsync"];


        //song 4
        [sonlis4 setTitle: @"Tush"];
        [sonlis4 setArtist: @"ZZtop"];

        //song5
        [sonlis5 setTitle: @"Jump"];
        [sonlis5 setArtist: @"Van Halen"];

        //song 6
        [sonlis6 setTitle: @"Aeroplane"];
        [sonlis6 setArtist: @"Red Hot Chilli Peppers"];

         //song 7
        [sonlis7 setTitle: @"Stand"];
        [sonlis7 setArtist: @"REM"];

         //song 8
        [sonlis8 setTitle: @"Betterman"];
        [sonlis8 setArtist: @"Pearl Jam"];

        //song 9
        [sonlis9 setTitle: @"Try"];
        [sonlis9 setArtist: @"Pink"];

        //song 10
        [sonlis10 setTitle: @"Madness"];
        [sonlis10 setArtist: @"Muse"];

        [myList addSong:sonlis1];
        [myList addSong:sonlis2];
        [myList addSong:sonlis3];
        [myList addSong:sonlis4];
        [myList addSong:sonlis5];
        [myList addSong:sonlis6];
        [myList addSong:sonlis7];
        [myList addSong:sonlis8];
        [myList addSong:sonlis9];
        [myList addSong:sonlis10];

        [myList listSongs];

    }
    return 0;
}

输出

2013-10-13 14:00:36.202 Final Project[1607:303] The list of my songs
2013-10-13 14:00:36.204 Final Project[1607:303] Holiday              by Madonna
2013-10-13 14:00:36.204 Final Project[1607:303] One                  by Metalica
2013-10-13 14:00:36.205 Final Project[1607:303] girlfriend           by Nsync
2013-10-13 14:00:36.205 Final Project[1607:303] Tush                 by ZZtop
2013-10-13 14:00:36.206 Final Project[1607:303] Jump                 by Van Halen
2013-10-13 14:00:36.206 Final Project[1607:303] Aeroplane            by Red Hot Chilli Peppers
2013-10-13 14:00:36.207 Final Project[1607:303] Stand                by REM
2013-10-13 14:00:36.207 Final Project[1607:303] Betterman            by Pearl Jam
2013-10-13 14:00:36.208 Final Project[1607:303] Try                  by Pink
2013-10-13 14:00:36.208 Final Project[1607:303] Madness              by Muse
4

2 回答 2

1

[myList sort]之前打电话[myList listSongs]

于 2013-10-13T21:36:03.787 回答
0

这里有很多问题:

  1. 你从不打电话[Library sort]
  2. 传递给的选择器[NSMutableArray sortUsingSelector]应该返回一个NSComparisonResult- 你正在使用[song Artist],它返回一个NSString. 看起来您实际上可能想要使用[Song compareArtist],它确实返回一个NSComparisonResult.
于 2013-10-13T21:36:42.803 回答