0

私はクラスオフィスサプライを持っています

@interface OfficeSupply : NSObject

@property (nonatomic, strong) NSString *itemName;
@property (nonatomic) int total;
@property (nonatomic) int price;
@property (nonatomic) int quantity;
@property (nonatomic, strong) UITextField *quantityText;
@property (nonatomic, strong) UIImage *itemPic;

-(id)initWithName:(NSString *)itemName total:(int)total price:(int)price quantity:(int)quantity picture:(UIImage *)itemPic;

@end

そして、私の見解では、

 OfficeSupply *item1 = [[OfficeSupply alloc] initWithName:@"Stapler" total:0 price:50 quantity:0 picture:[UIImage imageNamed:@"stapler.jpg"]];
OfficeSupply *item2 = [[OfficeSupply alloc] initWithName:@"Paper" total:0 price:150 quantity:0 picture:[UIImage imageNamed:@"101291.jpg"]];
OfficeSupply *item3 = [[OfficeSupply alloc] initWithName:@"Pen" total:0 price:45 quantity:0 picture:[UIImage imageNamed:@"pen.jpg"]];
_items =[NSArray arrayWithObjects:item1, item2, item3, nil];

[_items sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"itemName" ascending:YES selector:@selector(caseInsensitiveCompare:)]]];

すでに問題を調べましたが、問題を解決できませんでした。

最後の行はそれをソートすることになっていますが、なぜそうしないのかわかりません。

それらを名前で並べ替えたいだけです..何かアイデアはありますか?

4

3 に答える 3

3

NSArrayは不変であるため編集できないため、次のことを行う必要があります。

_items = [_items sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"itemName" ascending:YES selector:@selector(caseInsensitiveCompare:)]]];
于 2013-05-28T22:05:55.693 に答える