我在我的应用程序中有一个NSMutableArray,我这样启动:
H-FILE
NSMutableArray *noteBookContent;
@property (nonatomic, retain) NSMutableArray *noteBookContent;
Run Code Online (Sandbox Code Playgroud)
M-FILE
@synthesize noteBookContent;
Run Code Online (Sandbox Code Playgroud)
然后我有一个方法,我打开一个txt文件并将其内容读入临时NSString并将此NSString切片到不同的位,然后将其放入NSMutableArray.像这样:
NSString *tempTextOut = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:&error];
self.noteBookContent = [[[tempTextOut componentsSeparatedByString: @"\n[DIVIDER]\n"] mutableCopy] autorelease];
Run Code Online (Sandbox Code Playgroud)
我的大问题是如果我重复相同的过程几次会发生什么.在我将新数据读入之前是否需要发布noteBookContent?是否存在数据混乱的可能性,例如,如果一个noteBookContent有10个项目(全部称为FRUIT)而下一个noteBookContent有5个项目(全部称为SALAD),我最终可能会得到SALAD,SALAD,SALAD,SALAD,SALAD,水果,水果,水果,水果等?
很抱歉,如果这很明显,但我真的不明白当我将新数据读入已包含旧数据的NSMutableArray时会发生什么.
谢谢你的解释!
我想解析一个NSMutableArray,当我找到一些响应某些条件的对象时,我想将它们从数组中删除.
如果没有两个(或更多)数组用于该过程,我该怎么做?
对于那些会被诱惑说:嘿,只是不可能解析并从阵列中删除对象,我只能说,当我解析一个抽屉,我想从中删除过时的药物,我没有问题做它...当我找到一个,我把它丢弃,然后我找下一个药盒来检查.我不需要第二个抽屉.
我有一个对象数组,我想根据对象的某个值(即self.example.value)进行排序.我创建了多个可变数组:
NSMutableArray *array1, *array2, *array3 = [[NSMutableArray alloc] initWithObjects: nil];
然后使用for循环遍历原始数组.如果对象匹配条件(self.example.value == someValue).我将对象添加到上面创建的一个新数组中.但是,当我稍后开始使用数组时,我注意到它们是空的.使用调试器我注意到以下内容:
for (customClass *object in arrayOfObject){ //starting here the debugger has NONE of the arrays created above
if (object.value == someValue){//after performing this line, the debugger shows array1 in memory BUT nothing in it EVEN if the 'if' statement isn't TRUE.
[array1 addobject:object];
} else if (object.value == someOtherValue){//after performing this line, the debugger shows array2 in memory BUT nothing in it EVEN if …Run Code Online (Sandbox Code Playgroud) 我有一个NSMutableArray类型的对象NSMutableDictionary,
在NSMutableDictionary包含2个键
- 航空公司(String)
-Rating(整数)
我有一个NSMutableArray所有的对象,我需要的是总结所有航空公司重复对象的评级,一个例子:
Airline Rating
A 2
B 3
B 4
C 5
Run Code Online (Sandbox Code Playgroud)
最终结果数组将是A = 2,C = 5和B的总和等于7.
我的代码到目前为止:
for (int i = 0; i < arrayMealRating.count; ++i) {
NSMutableDictionary *item = [arrayMealRating objectAtIndex:i];
NSLog(@"item=%@",item);
for (int j = i+1; j < arrayMealRating.count; ++j)
{
if ([[item valueForKey:@"Airline"] isEqualToString:[arrayMealRating objectAtIndex:j]]){
NSMutableDictionary *item = [arrayMealRating objectAtIndex:j];
NSMutableDictionary *item1 = [arrayMealRating objectAtIndex:i];
NSInteger auxCount = [[item valueForKey:@"setMealRating"] integerValue] + [[item1 valueForKey:@"setMealRating"] …Run Code Online (Sandbox Code Playgroud) 我有NSMutableArray70个对象,它们NSMutableDictionary在每个字典中我有2个值:"distance"和"indexPath".我想NSMutableArray从小距离到最大距离进行排序.使用我粘贴的代码后,距离以KM为单位,数组看起来像这样:
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"distance" ascending:YES];
[branchDistance sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
Run Code Online (Sandbox Code Playgroud)
这是这种方法的结果:
(lldb) po [branchDistance objectAtIndex:0]
(id) $1 = 0x0d6ac240 {
"<NSIndexPath 0xd6cf180> 1 indexes [0]" = indexPath;
"8.078547" = distance;
}
(lldb) po [branchDistance objectAtIndex:1]
(id) $2 = 0x0d6a64a0 {
"<NSIndexPath 0xd6cf3b0> 1 indexes [1]" = indexPath;
"45.044069" = distance;
}
(lldb) po [bran(lldb) po [branchDistance objectAtIndex:2]
(id) $4 = 0x0d6cf550 {
"<NSIndexPath 0xd69b3f0> 1 indexes [2]" = indexPath;
"9.992081" = distance;
}
(lldb) …Run Code Online (Sandbox Code Playgroud) objective-c nsdictionary nsmutablearray nssortdescriptor ios
我正在使用a UITableView和for数组中的每个对象作为数据源UITableView,如果它们符合某个if语句,我将删除它们.我的问题是它只删除数组中的每个其他对象.
码:
UIImage *isCkDone = [UIImage imageNamed:@"UITableViewCellCheckmarkDone"];
int c = (tasks.count);
for (int i=0;i<c;++i) {
NSIndexPath *tmpPath = [NSIndexPath indexPathForItem:i inSection:0];
UITableViewCell * cell = [taskManager cellForRowAtIndexPath:tmpPath];
if (cell.imageView.image == isCkDone) {
[tasks removeObjectAtIndex:i];
[taskManager deleteRowsAtIndexPaths:@[tmpPath]
withRowAnimation:UITableViewRowAnimationLeft];
}
}
Run Code Online (Sandbox Code Playgroud)
这有什么问题?
我正在通过翻译我为Android游戏编写的一些Java代码来完成Objective-c的第一步(经过很长很长一段时间之后).似乎没有容器可以接受一个对象,而不是id先将它强制转换?还是有吗?
具体来说,这是我正在尝试使用的代码:
NSMutableArray *touchedBodies = [[NSMutableArray alloc] init];
// some additional code
if(![touchedBodies containsObject:(id)body]) {
[touchedBodies addObject:(id)body];
}
Run Code Online (Sandbox Code Playgroud)
该containsObject行传递正常,但在touchedBodies addObject:(id)b我收到"错误访问"错误.我试图添加的身体是合法的Box2D b2Body.
当我试图直接添加身体而不进行强制转换时:
[touchedBodies addObject:body];
Run Code Online (Sandbox Code Playgroud)
编译器抱怨"无法使用类型为b2Body*的左值初始化id类型的参数
我错过了什么?
我希望得到我的NSArray元素的平均值,并按以下方式将它们存储在一个新数组中.
NSArray* number;
NSMutableArray* Average;
[Average objectAtIndex:0]=[number objectAtIndex:0];
[Average objectAtIndex:1]=([number objectAtIndex:0] + [number objectAtIndex:1])/2;
[Average objectAtIndex:2]=([number objectAtIndex:0] + [number objectAtIndex:1] + [number objectAtIndex:2])/3;
Run Code Online (Sandbox Code Playgroud)
我正在考虑使用2 for循环
for (int i =0; i<[number count]; i++){
for (int j=0; j<i; j++){
temp+=[number objectAtIndex:j];
}
[Average addObject: temp/(i+1)];
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以用更有效的方式帮助我
谢谢
我有这样的数组
A = [@"aa",@"cc",@"bb",@"bb",@"cc",@"aa",@"cc"]
Run Code Online (Sandbox Code Playgroud)
我需要将其转换为
A = [@"x2 aa",@"x2 bb",@"x3 cc"]
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个NSMutableArray来保存一些对象,但我真的很难理解创建一个我main.m可以添加和访问的所有方法的正确语法.
我已经尝试@property在我希望数组存储的对象的接口中创建它,我也尝试创建一个自定义初始化方法,但在创建对象的方法之外,并将其添加到数组我在阵列名称上获得未声明的标识符错误.
我更习惯java,我可以创建所有方法都可以访问的数组但我明白Objective-C不会允许这样.有人可以帮我一把,给我一个代码示例来创建NSMutableArray我main.m可以访问的所有方法吗?
nsmutablearray ×10
objective-c ×8
ios ×4
iphone ×3
cocoa-touch ×2
nsarray ×2
arrays ×1
average ×1
cocoa ×1
nsdictionary ×1
parsing ×1
uitableview ×1
xcode ×1