一个真正的初学者问题.我有一个NSView子类,我在其中创建一个包含NSValues的NSMutableArray.当我想使用writetofile将数组写入文件时:atomatically:文件已创建,但它不包含可变数组确实包含的NSValues.有谁知道我如何成功地将这个可变数组写入文件?
谢谢
以下循环中似乎存在一些内存泄漏:
NSMutableArray *array1 = [[NSMutableArray alloc] init];
for(SomeClass *someObject in array2){ //has already been populated;
if (someObject.field == desiredValue){
[array1 addObject:someObject];
}
}
//EDIT:
//use array1 for very secret operations
[array1 release];
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?
是否有这样的东西NSMutableArray会直接采取双打而不将它们放入@""?
这是在我的.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self.scrollView addSubview:self.contentView];
self.scrollView.contentSize = self.contentView.bounds.size;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSMutableArray *arr = [[NSMutableArray alloc]init];
arr = [Singleton getArray];
NSString *str = [arr componentsJoinedByString:@"\n"];
summaryLabel.text = str;
}
Run Code Online (Sandbox Code Playgroud)
这是我的.h
@interface TotalViewController : UIViewController
{
UIScrollView *scrollView;
UIView *contentView;
}
@property (nonatomic, retain) IBOutlet UIScrollView * scrollView;
@property (nonatomic, retain) IBOutlet UIView * contentView;
@property (nonatomic,strong) IBOutlet UILabel * summaryLabel;
Run Code Online (Sandbox Code Playgroud)
我的标签连接到视图控制器,我的contentView连接到视图控制器,我的summaryLabel连接到视图控制器.我需要标签滚动而不是.
我有nsmutable数组,我想在特定索引处添加对象,如何检查特定索引处的数组是否有对象?
我这样做:
if ([self.myArray objectAtIndex:index] !=nil) {
Run Code Online (Sandbox Code Playgroud)
但我大部分时间都得到例外,因为"超出范围"
我真的很感谢你的帮助
我在ViewDidLoad方法中有一个包含以下元素的数组
inputArray = [NSMutableArray arrayWithObjects:@"car", @"bus", @"helicopter", @"cruiz", @"bike", @"jeep", nil];
Run Code Online (Sandbox Code Playgroud)
我还有一个UITextField用于搜索的元素.所以一旦我输入一些东西UITextField,我想检查该字符串是否存在"inputArray"或not.If它不与inputArray元素匹配然后从inputArray相应的元素.
for (NSString* item in inputArray)
{
if ([item rangeOfString:s].location == NSNotFound)
{
[inputArray removeObjectIdenticalTo:item];//--> Shows Exception
NSLog(@"Contains :%@",containsAnother);
}
}
Run Code Online (Sandbox Code Playgroud)
但此代码显示异常,与"removeobject:"相关的内容
例外:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFConstantString rangeOfString:options:range:locale:]: nil argument'
*** First throw call stack:
`
Run Code Online (Sandbox Code Playgroud) 我有一个简单的程序,需要记录并持久存储每次用户在表视图中选择项目时产生的2个简单数据.这两个数据是1)tap()的时间NSDate和2)tapped项目的名称(NSString).此时,此信息采用以下形式:
TimerEvent *latestTappedEvent = [[TimerEvent alloc] init];
latestTappedEvent.timeTapped = NSDate.date;
latestTappedEvent.activityTapped = tappedItem.itemName;
Run Code Online (Sandbox Code Playgroud)
这两个数据必须保持相互关联.
我的问题是:
如何按时间顺序将这些数据输入和输出plist?
在我的研究中,我只是变得更加困惑.对我来说,如何使用plist并不明显.最初,我认为我可以使用NSMutableDictionarywith latestTappedEvent.timeTapped作为键,latestTappedEvent.activityTapped作为值.但是当我尝试手动构建plist时,似乎不可能,而是想要一个字符串作为键.
如果有人能帮助我理解这一点,最好通过给出这些不同元素之间关系的图形表示,我将永远感激不尽.
Run Code Online (Sandbox Code Playgroud)Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'
.h
@property (nonatomic, strong) NSMutableArray *history;
Run Code Online (Sandbox Code Playgroud)
.m
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSString *de = [[self.history objectAtIndex:indexPath.row] objectForKey:@"des"];
NSString *dest = [de stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *jsonURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://example.com/rm_history.php?user_id=%@&destinations=%@",self.uID,dest]];
NSData *jsonData = [NSData dataWithContentsOfURL:jsonURL];
[self.history removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]
withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
NSLog(@"create");
// Create a new instance …Run Code Online (Sandbox Code Playgroud) 我正在使用NSPredicate进行过滤,
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(firstName CONTAINS %@ ) OR (lastName CONTAINS %@ )OR (UserName CONTAINS %@ ) ", myText,myText,myText];
NSArray *filtered = [responseArray filteredArrayUsingPredicate:predicate];
Run Code Online (Sandbox Code Playgroud)
它工作正常,但它区分大小写.我需要过滤应该不区分大小写.
我试过了,
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY firstName CONTAINS %@ ) OR (ANY lastName CONTAINS %@ )OR (ANY UserName CONTAINS %@ ) ", myText,myText,myText];
Run Code Online (Sandbox Code Playgroud)
但它会引发错误
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The left hand side for an ALL or ANY operator must be either an NSArray or an NSSet.'
Run Code Online (Sandbox Code Playgroud)
我无法理解这个答案中提到的是什么.任何人都可以建议我在上面的过滤中应该更改什么?
根据ARC,会removeAllObjects在NSMutableArray对的CPU(释放的对象逐个明确),比简单地做更强烈myArray = [NSMutableArray new];,只是让ARC赶上整个释放内存块上它的下一个版本是圆的?
我可以用两段代码对此进行测试,但由于优化等原因,我真的只是对机制感到好奇。从一个C角度来看,调用所有要释放的对象听起来更强烈,但也许deallocObjective-C 中的树可能足够高效,ARC 可以以相同的速度执行此操作?
nsmutablearray ×10
ios ×8
objective-c ×6
arrays ×1
cocoa ×1
cocoa-touch ×1
double ×1
ipad ×1
iphone ×1
memory-leaks ×1
nspredicate ×1
nsstring ×1
performance ×1
plist ×1
search ×1
singleton ×1
uitableview ×1
writefile ×1