当我收到内存警告时,我发布了一堆存储在NSMutableArray中的对象.
[_children release];
Run Code Online (Sandbox Code Playgroud)
我还需要在某些时候通过对象进行递归(可能在发生mem警告之后),所以我需要检查对象是否仍然存在,我与nil进行比较 - 这是不可行的,因为释放了数组不会有任何结果.
我无法循环遍历数组,逐个删除对象并设置为nil,因为我不允许在迭代期间修改数组.
感觉我错过了一些明显的东西.我只想在数组中释放+ nil一些值.
我试图从属性编辑一个可变数组,似乎无法直接使其工作.考虑以下似乎有用的代码,但似乎也非常低效; 我必须复制整个属性数组才能在一个对象中进行更改.为什么我可以更改整个"carNames"数组,但不能对其中一个对象进行更改?
感谢您提供的任何光线......
// CarTableViewController.h
// ...
@interface CarTableViewController : UITableViewController {
NSMutableArray *carNames;
}
@property (nonatomic, retain) NSMutableArray *carNames;
-(IBAction)addCarButton:(id)sender;
// ...
// --------------------------------------
// -------CarTableViewController.m-------
// ...
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Could I somehow run the removeOjectAtIndex:
// directly on the carNames Property? - this code works...
NSMutableArray *mutablecarNames = [carNames mutableCopy];
[mutablecarNames removeObjectAtIndex:indexPath.row];
carNames = [mutablecarNames copy];
[mutablecarNames release];
// This code doesn't work... Why?
// [carNames removeObjectAtIndex:indexPath.row]; …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用filteredArrayUsingPredicate与从.plist文件中的数据构建的数组.不知怎的,它似乎永远不会过滤我的数组.
这就是我的数组的构建方式:
DrillDownAppAppDelegate *AppDelegate = (DrillDownAppAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource = [AppDelegate.data objectForKey:@"Rows"];
copyDataSource = [ tableDataSource mutableCopy];
Run Code Online (Sandbox Code Playgroud)
然后我的谓词是这样的,
NSString *searchFor = search.text;
[tableDataSource release];
tableDataSource = [copyDataSource mutableCopy];
if ([searchFor length] > 0) {
NSLog(@"array = %@",tableDataSource);
NSPredicate *pred = [NSPredicate predicateWithFormat:@"Self beginswith[c] %@",searchFor];
[tableDataSource filteredArrayUsingPredicate:pred];
}
Run Code Online (Sandbox Code Playgroud) 我无法弄清楚造成这种情况的原因.基本上,一些不同的"任务"在我的应用程序中相互冲突.当我按下按钮时,它运行此代码就好了:
PalAppDelegate *dataCenter = (PalAppDelegate *)[[UIApplication sharedApplication] delegate];
[dataCenter.colourPalettesContainer addObject:[NSNumber numberWithInt:5]];
Run Code Online (Sandbox Code Playgroud)
它可以像我喜欢的那样多次这样做.但是,当我执行另一项任务(以及导致这种情况发生的一些任务)时,这涉及此代码:
PalAppDelegate *dataCenter = (PalAppDelegate *)[[UIApplication sharedApplication] delegate];
[dataCenter.colourPalettesContainer removeObjectAtIndex:touchDownID];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:dataCenter.colourPalettesContainer forKey:@"container"];
[prefs synchronize];
Run Code Online (Sandbox Code Playgroud)
然后:
dataCenter.colourPalettesContainer = [prefs objectForKey:@"container"];
Run Code Online (Sandbox Code Playgroud)
当我在此之后再次运行第一个代码时,它会导致"发送到不可变对象的变异方法"错误.我怎么能阻止这个?
编辑:所以我从下面的一些答案中找到了问题.有没有人有不同的做法,他们建议?
在Java中,我通常初始化特定大小的数组,然后在我的代码继续时添加和替换对象.在Objective C中,我不能用NSArray做到这一点.我从Java移植的代码通常必须使用NSMutableArray,我认为它的执行效率低于NSArray.(我最好猜测NSMutableArray如何存储它的成员是使用链表,但我可能是错的)
Objective C中是否有任何类型的数组具有固定的大小并允许在数组中进行更改?为什么不能用NSArray替换某个对象的对象?我可以用C数组做到这一点,为什么不用Objective C?
我试图检查一个NSArray,如果它包含任何特定索引的任何对象,并在检查它时,我想在该特定索引处插入对象,如果它是空的.有关如何做到这一点的任何想法?
我是一名新的iPad开发人员.
我按下按钮实现UIPopover,popover包含整数值,
当我试图用整数填充我的数组时它向我显示SIGABRT:索引3超出界限我无法看到我的日志,应用程序崩溃.
这是我的代码片段:
-(void)btnClick:(id)sender {
for (int i=3; i<=31; i++) {
[remindarray addObject:[NSNumber numberWithInteger:i]];
NSLog(@"no=%@",[remindarray objectAtIndex:i]);
}
UIViewController* popoverContent = [[UIViewController alloc]init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(110, 0, 500, 4)];
popoverTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 250, 665) style:UITableViewStylePlain];
[popoverTable setDelegate:(id<UITableViewDelegate>)self];
[popoverTable setDataSource:(id<UITableViewDataSource>)self];
[self.view addSubview:popoverTable];
[popoverTable release];
[popoverView addSubview:popoverTable];
popoverContent.view = popoverView;
popoverContent.contentSizeForViewInPopover = CGSizeMake(250, 600);
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:popoverContent];
[self.popoverController presentPopoverFromRect:CGRectMake(100,0, 535, 35)
inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
[popoverView release];
[popoverContent release];
}
Run Code Online (Sandbox Code Playgroud)
最后remind …
我是iOS开发新手,无法比较两个NSMutableArray.
其中一个(appDelegate.array代码中)包含来自数据库的数据,另一个(dataArray代码中)包含来自服务器响应的数据.
现在我想比较(dataArray)的每个元素与whole(appDelegate.array),如果(dataArray)中存在(appDelegate.array)的某些元素,则不执行任何操作或中断,如果不存在则将其添加到数据库中.
我试过了,但我无法做到这一点.下面是我正在使用的代码.
先感谢您.
NSMutableArray *dataArray = [responseDictionary objectForKey:@"myDATA"];
NSLog(@"%d dataArray count", [dataArray count]);
for (int i = 0; i < [dataArray count]; i++)
{
NSLog(@"%d delegate array count",[appDelegate.array count]);
NSInteger ID = [[[dataArray objectAtIndex:i] objectForKey:@"ID"] intValue];
NSString *Note = [[dataArray objectAtIndex:i] objectForKey:@"Note"];
NSString *Reminder = [[dataArray objectAtIndex:i] objectForKey:@"Reminder"];
NSInteger Status = [[[dataArray objectAtIndex:i] objectForKey:@"Completed"]intValue];
NSInteger DisplayOrder = [[[dataArray objectAtIndex:i] objectForKey:@"Display_Order"] intValue];
if ([appDelegate.array count] …Run Code Online (Sandbox Code Playgroud) 我有NSMutableArray如下.
(
{
ClientId = "17";
FileURL = "http://www.xxx.com/b0a89db3-482b-464e-81ae-78439527c702.jpg";
},
{
ClientId = "56";
FileURL = "http://www.xxx.com/f7633371-b726-4332-9b26-a98a2fed991d.jpg";
}
)
Run Code Online (Sandbox Code Playgroud)
我想要的是NSMutableArray,它看起来类似于下面.
NSMutableArray *image_array =
[NSMutableArray arrayWithObjects:
@"http://www.xxx.com/b0a89db3-482b-464e-81ae-78439527c702.jpg",
@"http://www.xxx.com/f7633371-b726-4332-9b26-a98a2fed991d.jpg",
nil];
Run Code Online (Sandbox Code Playgroud)
知道怎么做吗?
我试过了
NSArray *filtered = [feeds filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(FileURL CONTAINS[cd] %@)", @"%http%"]];
NSLog(@"filter====%@", filtered);
Run Code Online (Sandbox Code Playgroud)
但我没有得到任何东西.数据为空白.
实际上我有以下格式的主阵列.
(
{
ClientId = "17\n ";
FileURL = "http://www.xxx.com/b0a89db3-482b-464e-81ae-78439527c702.jpg\n\n";
},
{
ClientId = "56\n ";
FileURL = "http://www.xxx.com/f7633371-b726-4332-9b26-a98a2fed991d.jpg\n\n";
}
)
Run Code Online (Sandbox Code Playgroud)
知道如何\n在创建新数组时删除那些和空格?
这应该是相当容易的,可能我只是遗漏了一件小事:我有一个名为defaultImages的图像数组
NSMutableArray *defaultLetters;
Run Code Online (Sandbox Code Playgroud)
然后我添加所需的字母(数组长度然后是42)
后来我试图用另一个图像替换阵列中的一个图像.像这样:
[defaultLetters replaceObjectAtIndex:0 withObject:croppedPhoto];
Run Code Online (Sandbox Code Playgroud)
但我得到一个错误说:" - [__ NSArrayI replaceObjectAtIndex:withObject:]:无法识别的选择器发送到实例0x180ae190"是否有一个我缺少的步骤?
nsmutablearray ×10
objective-c ×7
iphone ×6
ios ×3
arrays ×2
compare ×1
for-loop ×1
integer ×1
ipad ×1
java ×1
nsarray ×1
nspredicate ×1
null ×1
properties ×1
release ×1
uitableview ×1