Xcode Instruments声称以下代码会导致内存泄漏.据我所知,在分配属性时会发生以下情况:
*旧值是自动释放的
*保留
新值*显然分配了新值
随着它的介意,我怎么会有内存泄漏,我该如何解决它?
"TestProjectViewController.h":
#import <UIKit/UIKit.h>
@interface TestProjectViewController : UIViewController {
NSMutableArray* array;
}
@property (nonatomic, retain) NSMutableArray* array;
@end
Run Code Online (Sandbox Code Playgroud)
"TestProjectViewController.m":
#import "TestProjectViewController.h"
@implementation TestProjectViewController
@synthesize array;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
for(int i = 0; i < 5; i++) {
self.array = [[NSMutableArray alloc] init];
[self.array addObject:@"Hello world #1"];
[self.array addObject:@"Hello world #2"];
}
}
Run Code Online (Sandbox Code Playgroud) 我试图将一些数据从NSMutableArray写入一个plist,同时保留旧的plist内容.
该函数writeToFile:atomically:用new覆盖旧内容,我想将新数组中的对象追加到plist中.
如何才能做到这一点?谢谢.
这样做时如何检查重复项?
我有一个NSStrings的数组(实际上是一个可变数组),我想知道数组中有多少个唯一元素.
例如,假设数组由以下组成:
Orange, Lemon, Lemon, Orange, Lemon
Run Code Online (Sandbox Code Playgroud)
然后只有两个独特的元素(橙色和柠檬).这个数组:
Paul, Steve, John, Harry, Paul, John
Run Code Online (Sandbox Code Playgroud)
..有四个独特的独特元素.
我如何发现这个号码?
我正在寻找可能是我新手问题的简单答案.我知道NSMutableArray只能保存对象,所以当我将double放入数组时,我使用了[myArray addObject:[NSNumber numberWithDouble:aValue]];转换方法.我现在需要做的是将值拉出来作为双倍并将它们平均.我相信我将使用快速枚举方法与FoR循环.
我只需要知道如何让以下工作.
double total;
total = [myArray objectAtIndex:i]
Run Code Online (Sandbox Code Playgroud) Car class
--------------
price
color
Run Code Online (Sandbox Code Playgroud)
崩溃代码是:
NSMutableArray *list = [[NSMutableArray alloc] init];
Car *car = [[Car alloc] init];
car.price = 10;
car.color = 1;
[list addObject:car];
// some code
[list removeAllObjects]; // Crash here
Run Code Online (Sandbox Code Playgroud)
为什么崩溃,我该如何解决它.
app退出没有任何输出
我试图将NSString转换(或复制?)为NSMutableArray.我想我的问题是我并不真正了解MutableArray的结构.在我有限的知识中,数组可能如下所示:
NoteBook = [[NSMutableArray alloc] init];
for (int temp = 0; temp < 3; temp++) {
[NoteBook insertObject:@"Page" atIndex:temp];
}
Run Code Online (Sandbox Code Playgroud)
哪个会给我一个PagePagePage数组.我们假设我想打开一个包含PagePagePage的txt文件,但是这些单词被一个已定义的字符串分开,这样我就可以将我的数组中的各个对象分开,如下所示:Page ---页面末尾--- Page- - 页面末尾---页面.
现在,我的下一步是从txt文件中读取此信息:
NSString *tempTextOut = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:&error];
NoteBook = [tempTextOut componentsSeparatedByString: @"\n--- end of page ---\n"];
Run Code Online (Sandbox Code Playgroud)
但是,最后一行不起作用,我告诉xCode:不兼容的Objective-C类型分配'struct NSArray*',期望'struct NSMutableArray*'.我真的不明白这一点 - NSArray和MutableArray应该是兼容的(因为一个是另一个的子类).不应该xCode告诉我问题是我一直在尝试将NSString转换为NSMutableArray吗?
我可能需要在重新设置MutableArray之前重新设置它,因为现在它仍然包含我在第一步中分配给它的PagePagePage.我以为我的NoteBook可变数组会简单地被字符串替换,但我想情况并非如此.
我非常感谢这件事的任何帮助.谢谢!
NSMutableArray *sortedReleases = [theReleases sortedArrayUsingComparator:^(id a, id b)
Run Code Online (Sandbox Code Playgroud)
我得到以下内容:
不兼容的指针类型使用"NSArray*"类型的表达式初始化"NSMutableArray*"
我必须将sortedReleases作为NSMutableArray.我如何通过它?
谢谢
我正在浏览斯坦福大学的iOS开发视频并正在完成作业1.
我遇到任务4的问题:
向用户界面添加一个新的文本标签(UILabel),显示已发送到大脑的所有内容(以空格分隔).例如,如果用户输入6.3输入5 + 2*,则此新文本标签将显示6.3 5 + 2*.放置此标签的好地方是使其成为显示文本标签上方的细条.别忘了让C按钮清除.此任务的所有代码都应该在您的Controller中(此模型不需要对您的模型进行任何更改).您不必显示无限数量的操作和操作数,只需合理的数量.
我有UILabel并完成了所有这些工作.我发现我很难做到这一点.任何帮助是极大的赞赏.
(PS请不要发布完整的代码解决方案,因为我想学习如何做,发布代码只会让我的学习变得毫无意义,任何提示和指针都是最好的!谢谢).
我使用Core Data并将对象提取到NSMutableArray中:
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
self.items = [[managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];
Run Code Online (Sandbox Code Playgroud)
然后我使用这个可变数组在表视图中显示对象.此外,该数组中的数据随时间变化.我想在Core Data中坚持整个数组.
我最初的想法是从Core Data中删除所有项目,并通过对可变数组中的所有对象进行迭代,一次只保留一个:
- (void)persistAllItemsInCoreData{
[self clearAllItemsFromCoreData];
NSManagedObjectContext *context = [self managedObjectContext];
for(int i = 0 ; i < [items count] ; i++){
Item *item = [NSEntityDescription
insertNewObjectForEntityForName:@"Item"
inManagedObjectContext:context];
item = [items objectAtIndex:i];
}
NSError *error;
if (![context save:&error]) {
NSLog(@"Could not save data: %@", [error localizedDescription]);
}
}
Run Code Online (Sandbox Code Playgroud)
然而,这不起作用.有没有更优雅的方法来使用Core Data持久保存NSMutableArray?
是否可以调用NSMutableArray的addObject:方法?
这是我正在尝试的.
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@implementation NSMutableArray (LoggingAddObject)
+ (void)load {
Method addObject = class_getInstanceMethod(self, @selector(addObject:));
Method logAddObject = class_getInstanceMethod(self, @selector(logAddObject:));
method_exchangeImplementations(addObject, logAddObject);
Method original = class_getInstanceMethod(self, @selector(setObject:atIndexedSubscript:));
Method swizzled = class_getInstanceMethod(self, @selector(swizzled_setObject:atIndexedSubscript:));
method_exchangeImplementations(original, swizzled);
}
- (void)logAddObject:(id)anObject {
[self logAddObject:anObject];
NSLog(@"Added object %@ to array %@", anObject, self);
}
-(void)swizzled_setObject:(id)obj atIndexedSubscript:(NSUInteger)idx
{
NSLog(@"This gets called as expected!!-----");
[self swizzled_setObject:obj atIndexedSubscript:idx];
}
Run Code Online (Sandbox Code Playgroud)
我能够调整一些像setObject:atIndexedSubscript这样的方法:但我担心我不能这样做addObject:和其他人.我觉得下面不能调整?有人可以解释原因吗?我做错了什么或者解决这个问题?
/**************** Mutable Array ****************/
@interface NSMutableArray : NSArray
- (void)addObject:(id)anObject;
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
- (void)removeLastObject;
- …Run Code Online (Sandbox Code Playgroud) nsmutablearray ×10
objective-c ×8
ios ×5
iphone ×5
append ×1
cocoa-touch ×1
core-data ×1
double ×1
file ×1
memory-leaks ×1
nsarray ×1
nsstring ×1
stanford-nlp ×1