一个真正的初学者问题.我有一个NSView子类,我在其中创建一个包含NSValues的NSMutableArray.当我想使用writetofile将数组写入文件时:atomatically:文件已创建,但它不包含可变数组确实包含的NSValues.有谁知道我如何成功地将这个可变数组写入文件?
谢谢
我可以删除我在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 =零?
我有一个名为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 *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)
有什么想法吗?
目前我正试图找到一种平均矩阵的简洁方法.显而易见的解决方案是对矩阵求和,然后除以元素的数量.但是,我在苹果开发者网站上遇到了一种方法,声称可以使用valueForKeyPath以更简单的方式完成此操作.这与以下内容相关联:
以下是我正在努力尝试使其工作的示例:
-(void)arrayAverager
{
NSMutableArray *myArray = [NSMutableArray arrayWithCapacity:25];
[myArray addObject:[NSNumber numberWithInteger:myValue]];
NSNumber *averageValue = [myArray valueForKeyPath:@"@avg.self"];
NSLog(@"avg = %@", averageValue);
}
Run Code Online (Sandbox Code Playgroud)
的问题是:代替平均它仅通过1打印出阵列1中的元素阵列.
UPDATE
-(void) pixelAverager
{
//Put x-coordinate value of all wanted pixels into an array
NSMutableArray *xCoordinateArray = [NSMutableArray arrayWithCapacity:25];
[xCoordinateArray addObject:[NSNumber numberWithInteger:xCoordinate]];
NSLog(@"avg = %@", [xCoordinateArray valueForKeyPath:@"@avg.intValue"]);
}
Run Code Online (Sandbox Code Playgroud) 我有2个NSMutableArrays.第一个数组包含带属性的自定义对象intances NSString *itemID,第二个数组仅包含NSString具有相同值的对象itemID,但是以另一个顺序包含.我需要按itemID每个对象的属性排序第一个数组,它应该像第二个数组一样排序.
我怎么能这样做?
我试图从一些地方得到的数据,并将其保存在一个NSMutableArray包含数据和信息自定义对象.
每当用户打开相同的数据是从源加载,并转换为我的可爱阵列自定义对象的应用程序.这需要很长的时间(〜10-20s),使很愚蠢的应用程序.所以我试图获取数据并在此链接中显示它
我尝试了什么:
NSUserDefault.在任何人开始回答这个问题之前.看看示例代码并运行它以确保检索数据并显示它的含义.在重新启动样品后的快速时间,使我想要更清楚:)
如果有人能让我的样本更加清晰,我会感激不尽.
我有一个简单的程序,需要记录并持久存储每次用户在表视图中选择项目时产生的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) nsmutablearray ×10
ios ×6
objective-c ×6
iphone ×3
cocoa ×2
average ×1
for-loop ×1
ios4 ×1
ios5 ×1
ios6 ×1
memory-leaks ×1
nsarray ×1
plist ×1
sorting ×1
uitableview ×1
writefile ×1