无法将对象添加到可变数组 [英] Adding an object to Mutable array is not possible
问题描述
我试图将Song *对象添加到Mutable数组中,但我感到很困惑,因为尽管添加了对象,但数组的数量却没有增加.
I am trying add a Song* object to a Mutable array and I am stumped as the count of the array is not increasing in spite of adding the object.
Song.h
#import <Foundation/Foundation.h>
@interface Song : NSObject
@property(copy, nonatomic) NSString *title, *album, *artist;
@property(copy, nonatomic) NSNumber *playTime;
@end
Song.m
#import "Song.h"
@implementation Song
@end
播放列表.h
#import <Foundation/Foundation.h>
@class Song;
@interface Playlist : NSObject
@property(copy, nonatomic) NSMutableArray *playListArray;
-(void) addSong: (Song *) tempSongToBeAdded;
-(void) removeSong: (Song *) tempSongToBeremoved;
-(void) listOfSongs;
-(NSUInteger) entries;
@end
播放列表.m
#import "Playlist.h"
#import "Song.h"
@implementation Playlist
-(void) addSong: (Song *) tempSongToBeAdded{
NSLog(@"%s song is being added.", [tempSongToBeAdded.title UTF8String]);
[self.playListArray addObject:tempSongToBeAdded];
}
-(void) removeSong: (Song *) tempSongToBeremoved{
[self.playListArray removeObject:tempSongToBeremoved];
}
-(NSUInteger) entries{
return [self.playListArray count];
}
-(void) listOfSongs{
NSLog(@"Hi");
for (Song *tempSong in self.playListArray) {
NSLog(@"title: %s", [tempSong.title UTF8String]);
}
}
@end
Main.m
#import <Foundation/Foundation.h>
#import "Song.h"
#import "Playlist.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Song *song1 = [[Song alloc] init];
song1.title = @"Manasa";
song1.album = @"Ye Maya Chesava";
song1.artist = @"A. R. Rahman";
song1.playTime = [NSNumber numberWithDouble:4.56];
Playlist *playlist1 = [[Playlist alloc] init];
[playlist1 addSong:song1];
NSLog(@"The total number of songs are %lu",[playlist1 entries]);
[playlist1 listOfSongs];
}
return 0;
}
我在播放列表中获得的条目为0,在歌曲列表中获得的条目为空.我没有收到任何编译错误,也不知道为什么没有将对象添加到数组中.
I am getting the entries in the playlist as 0 and and the list of songs as empty. I am not getting any compile errors and I have no idea why the objects are not getting added to the array.
推荐答案
您的变量playListArray
从未初始化,并且始终为nil
.您需要使用以下命令对其进行初始化:
Your variable playListArray
is never initialized and is always nil
. You need to initialize it using:
playListArray = [[NSMutableArray] alloc] init];
您可以在初始化该对象的Playlist
类中添加init
方法.
You can add an init
method in your Playlist
class where you initialize this object.
- (id)init
{
self = [super init];
if (self)
{
playListArray = [[NSMutableArray] alloc] init];
}
return self;
}
问题似乎在于您如何声明该属性
The problem seems to be how you declared the property
@property(copy, nonatomic) NSMutableArray *playListArray;
它声明为copy
,这意味着即使执行playListArray = [[NSMutableArray] alloc] init]
,变量playListArray
也会获取初始化数组的副本,但是copy
协议是从NSArray
继承的,而不是从NSMutableArray
,因此您获得了一个不可变的数组.您可以在NSMutableArray
文档中进行检查.您需要将copy
更改为retain
(您不使用ARC,对吗?).
It is declared as copy
, that means that even when you do playListArray = [[NSMutableArray] alloc] init]
, your variable playListArray
gets a copy of the initialized array, but the copy
protocol is inherited from NSArray
, not from NSMutableArray
, so you get an immutable array. You can check this in the NSMutableArray
documentation. You need to change copy
for retain
(you're not using ARC, right?).
事实上,我发现您对大多数属性都使用copy
,如果没有特殊原因,我会将其更改为retain
.
In fact I see that you're using copy
for most of your properties, if there's no particular reason for this, I would change them to retain
.
这篇关于无法将对象添加到可变数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!