我正在尝试创建数组(Cities)的数组(States).每当我尝试将项目添加到我的City数组时,我都会收到此错误:
'NSInvalidArgumentException',原因:'***+ [NSMutableArray addObject:]:无法识别的选择器发送到类0x303097a0
我的代码如下.它错误的行是
[currentCities addObject:city];
Run Code Online (Sandbox Code Playgroud)
我确定我有一些内存管理问题,因为我仍然不太了解它.希望有人可以向我解释我的错误.
if (sqlite3_prepare_v2(db, sql, -1, &statement, NULL) == SQLITE_OK){
// We need to keep track of the state we are on
NSString *state = @"none";
NSMutableArray *currentCities = [NSMutableArray alloc];
// We "step" through the results - once for each row
while (sqlite3_step(statement) == SQLITE_ROW){
// The second parameter indicates the column index into the result set.
int primaryKey = sqlite3_column_int(statement, 0);
City *city = [[City alloc] initWithPrimaryKey:primaryKey database:db];
if (![state isEqualToString:city.state])
{ …Run Code Online (Sandbox Code Playgroud) 我有一个加载值的NSMutableArray.
稍后在应用程序中,我需要更改数组中其中一个元素的值.
我该如何做到这一点?
谢谢托尼
我可以删除我在Objective-C for循环中循环的项目而没有副作用吗?
例如,这样可以吗?
for (id item in items) {
if ( [item customCheck] ) {
[items removeObject:item]; // Is this ok here?
}
Run Code Online (Sandbox Code Playgroud) for-loop objective-c nsmutablearray nsarray fast-enumeration
如果我有一个带有10个对象的NSMutableArray
我运行这行代码
[tempArray removeObjectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)
然后
[tempArray count] 应该返回9
但是整个阵列都会向上移动
对象在索引1处移动到索引0
对象在索引2处移动到索引1
...
索引9处的对象移动到索引8
或指数0 =零?
在创建数组时没有必要指定数组大小,对吗?那么,为什么arrayWithCapacity必要?如果我将数组的大小设置为小于实际需要的数量,那可以吗?
我有一个名为Occasion的自定义对象,定义如下:
#import <Foundation/Foundation.h>
@interface Occasion : NSObject {
NSString *_title;
NSDate *_date;
NSString *_imagePath;
}
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSDate *date;
@property (nonatomic, retain) NSString *imagePath;
Run Code Online (Sandbox Code Playgroud)
现在我有一个NSMutableArray of Occasions,我想保存到NSUserDefaults.我知道这是不可能的,所以我想知道哪种方法最简单?如果序列化是答案,那么如何?因为我阅读了文档但却无法理解它的完整工作方式.
iphone serialization custom-object nsuserdefaults nsmutablearray
有什么区别:
[[NSMutableArray alloc] init]
Run Code Online (Sandbox Code Playgroud)
和
[NSMutableArray array]
Run Code Online (Sandbox Code Playgroud) 我有2个NSMutableArrays.第一个数组包含带属性的自定义对象intances NSString *itemID,第二个数组仅包含NSString具有相同值的对象itemID,但是以另一个顺序包含.我需要按itemID每个对象的属性排序第一个数组,它应该像第二个数组一样排序.
我怎么能这样做?
我试图从一些地方得到的数据,并将其保存在一个NSMutableArray包含数据和信息自定义对象.
每当用户打开相同的数据是从源加载,并转换为我的可爱阵列自定义对象的应用程序.这需要很长的时间(〜10-20s),使很愚蠢的应用程序.所以我试图获取数据并在此链接中显示它
我尝试了什么:
NSUserDefault.在任何人开始回答这个问题之前.看看示例代码并运行它以确保检索数据并显示它的含义.在重新启动样品后的快速时间,使我想要更清楚:)
如果有人能让我的样本更加清晰,我会感激不尽.
我发现了类似的问题,但是-containsObject没有像我期望的那样工作.
我的问题是NSMutableArray -containsObject当它不应该,当尝试生成随机的UNIQUE颜色并添加到数组时,该方法返回true.
检查是否NSMutableArray包含具有相同值的对象的最佳方法是什么.
NSMutableArray *color_arr=[NSMutableArray array];
UIColor *t;
for(int i=0; i<100; i+=1)
{
int r = arc4random()%256;
int g = arc4random()%256;
int b = arc4random()%256;
t=[UIColor colorWithRed:r green:g blue:b alpha:255];
if (![color_arr containsObject:t])
[color_arr addObject:t];
//[t release];//is t need to be released here on non-arc project? well Im not sure.
}
NSLog(@"total:%d",[color_arr count]);
Run Code Online (Sandbox Code Playgroud)
NSLog()总说数组数为1.