无法进行数组排序

时间:2013-10-13 21:24:56

标签: objective-c xcode sorting

我自学了大约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

MAIN

#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

2 个答案:

答案 0 :(得分:1)

[myList sort]之前致电[myList listSongs]

答案 1 :(得分:0)

这里有很多问题:

  1. 您永远不会致电[Library sort]
  2. 传递给[NSMutableArray sortUsingSelector]的选择器应该返回NSComparisonResult - 您正在使用[song Artist],它会返回NSString。看起来您可能确实想要使用[Song compareArtist],它确实会返回NSComparisonResult