在调试同事编写的代码的过程中,我偶然发现了令我神秘化的以下内容:
NSMutableArray *array = [NSMutableArray array];
NSUInteger arrayCount = array.count;
Run Code Online (Sandbox Code Playgroud)
为什么这样做?它也适用于NSDictionary和其他类型,但在文档和Cocoa标题中没有任何地方@property可以找到这些定义.
谷歌搜索"NSArray属性"并没有产生许多有用的结果,所以我正在向SO伸出援手,这肯定是一个非常令人尴尬的问题.
执行时
[self.blockViews addObject:curBlockView];
Run Code Online (Sandbox Code Playgroud)
我收到一个错误
2011-07-01 13:35:26.240 Block Breaker [42061:207] - [__ NSArrayI addObject:]:无法识别的选择器发送到实例0x4e037a0
我对Objective-C很新.这是我的init方法吗?
//
// GameEngine.h
// Block Breaker
//
// Created by Chris Muench on 7/1/11.
// Copyright 2011 N/A. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface GameEngine : NSObject {
NSMutableArray *blockViews;
int numBlockRows;
int score;
}
@property (nonatomic, copy) NSMutableArray *blockViews;
@property int numBlockRows;
@property int score;
- (void) setup;
- (void) setupBlocks;
@end
//
// GameEngine.m
// Block Breaker
//
// Created by …Run Code Online (Sandbox Code Playgroud) 我正在尝试将数据保存到Iphone上的XML文件中.为此,我加载整个XML,添加新数据并再次保存.我尝试存储新数据时会出现问题
[mArray addObject:newData];
Run Code Online (Sandbox Code Playgroud)
方法崩溃,因为mArray不是NSMutableArray,相反,即使我对它应用了mutableCopy方法,它也是一个NSCFArray.据我所知,NSCFArray是NSArray的免费桥接,因此我无法理解为什么mutablyCopy方法不起作用.任何的想法??
NSMutableDictionary *wholeXML = [[NSMutableDictionary alloc] init];
wholeXML = xmlData;
NSArray *array = [[NSArray alloc] init];
NSMutableArray *mArray = [[NSMutableArray alloc] init];
array = [wholeXML objectForKey:@"Key"];
mArray = [a mutableCopy];
Run Code Online (Sandbox Code Playgroud) 我有一个'通用对象'的NSArray,它包含以下属性
-name
-id
-type (question, topic or user)
Run Code Online (Sandbox Code Playgroud)
如何根据泛型对象的类型对这个泛型对象数组进行排序?例如,我想在顶部显示"主题"类型的所有通用对象,然后是"用户"而不是"问题"
我想存储2个int的元组和NSArray中的char.有没有比宣布持有2个整数和char的类更简单的方法?
我用这种方式试了它并且它有效,但对我来说似乎相当复杂.有更好,更轻松的方式吗?
@interface Container : NSObject
@property NSInteger a;
@property NSInteger b;
@property char c;
@end
@implementation Container
@synthesize a = _a;
@synthesize b = _b;
@synthesize c = _c;
-(Container*) initWitha:(NSInteger) a andB:(NSInteger) b andC: (char) c
{
if ((self = [super init])) {
self.a = a;
self.b = b;
self.c = c;
}
return self;
}
@end
...
//usage
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject: [[Container alloc] initWitha:5 andB:6 andC:'D']];
Run Code Online (Sandbox Code Playgroud)
谢谢
关于NSArray我有两个问题:
[NSNull
null]正确吗?谢谢!!
在这里使用基本目标c示例,尝试使用replaceObjectAtIndex作为数组,它似乎不起作用.
我的代码:
NSMutableArray *myArray=[NSMutableArray array];
[myArray addObject:@"First string"];
[myArray addObject:@"Second string"];
[myArray addObject:@"Third string"];
NSString *newElement=[myArray objectAtIndex:1];
NSLog(@"New object at index 1 BEFORE is %@", newElement);
[myArray replaceObjectAtIndex:1 withObject:@"Hello"];
NSLog(@"New object at index 1 AFTER is %@", newElement);
Run Code Online (Sandbox Code Playgroud)
从理论上讲,newElement的输出现在应该显示"Hello",但它仍然显示"Second String"
输出:
2012-05-30 11:21:16.638 cocoa lab[753:403] New object at index 1 BEFORE is Second string
2012-05-30 11:21:16.641 cocoa lab[753:403] New object at index 1 AFTER is Second string
Run Code Online (Sandbox Code Playgroud)
请指教
谢谢
我想根据或以某些字符串开头过滤一个User对象数组(User有fullname,user_id以及一些更多属性..).
我知道如何根据一个条件过滤: firstNamelastName
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"firstName BEGINSWITH[cd] %@", word];
NSArray* resArr = [myArray filteredArrayUsingPredicate:predicate];
Run Code Online (Sandbox Code Playgroud)
这将为我提供具有以"word"开头的firstName的所有用户.
但是如果我希望所有拥有firstName或lastName以"word"开头的用户怎么办?
当我使用这段代码时,Xcode给了我一个错误:
NSArray *buttonsFiles = [NSArray arrayWithObject:@"button_plus.png",@"button_minus.png",@"button_multiple.png",@"button_div.png",@"button_eq.png",@"button_percent.png",@"button_sqrt.png",@"button_back.png",@"button_pointer.png",@"button_c.png",@"button_0.png",@"button_1.png",@"button_2.png",@"button_3.png",@"button_4.png",@"button_5.png",@"button_6.png",@"button_7.png",@"button_8.png",@"button_9.png",nil];
Run Code Online (Sandbox Code Playgroud)
我应该使用它来避免错误吗?
NSString *myfile1 = [@"button_plus.png"];
NSString *myfile2 = [@"button_minus.png"];
...
NSString *myfile20 = [@"button_9.png"];
NSArray* myArray = [NSArray arrayWithObjects:myfile1, myfile2... myfile20r, nil];
Run Code Online (Sandbox Code Playgroud)

我经常需要更改一个NSArray或一个的内容NSDictionary.通常我会这样:NSDictionary- >创建NSMutableDictionary并编辑它 - >用编辑过的原件替换原件NSDictionary(作为不可变的).
我知道有两种方法可以做到这一点:
1)使用mutable copy和copy:
NSDictionary *myDictionary = @{...};
NSMutableDictionary *dicToEdit = myDictionary.mutableCopy;
// Editing goes here`
myDictionary = dicToEdit.copy;
Run Code Online (Sandbox Code Playgroud)
2)使用arrayWithArray:或dictionaryWithDictionary:
NSDictionary *myDictionary = @{...};
NSMutableDictionary *dicToEdit = [NSMutableDictionary dictionaryWithDictionary:myDictionary];
// Editing goes here
myDictionary = [NSDictionary dictionaryWithDictionary:dicToEdit];
Run Code Online (Sandbox Code Playgroud)
这两种方式有什么区别吗?或者这两种方式中的一种有问题吗?
objective-c nsdictionary nsmutablearray nsarray nsmutabledictionary
nsarray ×10
objective-c ×10
ios ×4
properties ×3
iphone ×2
nsdictionary ×2
arguments ×1
arrays ×1
cocoa ×1
nspredicate ×1
nsstring ×1
sorting ×1