简体   繁体   English

iOS ivar分配/初始化将内存地址分配给另一个ivar

[英]iOS ivar alloc/init assigns memory address to another ivar

I've declared two NSMutableArrays in the interface of one of my view controllers, as you can see in the code below. 我在一个视图控制器的接口中声明了两个NSMutableArrays,如下面的代码所示。 However, after synthesizing the properties, I attempt to initialize my class' ivars in viewDidLoad. 但是,综合属性后,我尝试在viewDidLoad中初始化类的ivars。 If I step over this process in debugger, I see that it is offsetting memory allocation by one ivar, if that makes any sense. 如果我在调试器中逐步执行此过程,我会发现它可以使一个ivar抵消内存分配,如果这有意义的话。 So, for example, as I am initializing the first NSMutableArray, I can watch the memory address get assigned to the other NSMutableArray ivar. 因此,例如,当我初始化第一个NSMutableArray时,我可以看到将内存地址分配给另一个NSMutableArray ivar。 The NSMutableArray that the code should be initializing remains a null memory address. 代码应初始化的NSMutableArray仍为空内存地址。 This odd "offset" in memory allocations is try for initializing the second NSMutableArray too...it is initialized in my DataHelper ivar's memory location. 内存分配中的这个奇怪的“偏移量”也试图初始化第二个NSMutableArray……在我的DataHelper ivar的内存位置中对其进行了初始化。 This is causing errors to be thrown later in the code like: 这导致稍后在代码中引发错误,例如:

-[DataHelper count]: unrecognized selector sent to instance

Where it is expecting a reference to an NSMutableArray in memory, it is instead pointing to the DataHelper memory address. 在期望引用内存中的NSMutableArray的地方,它指向的是DataHelper内存地址。 Has anyone ever experienced anything like this? 有没有人经历过这样的事情? I'm using ARC, the latest version of Xcode, and iOS 5.1. 我正在使用ARC,最新版本的Xcode和iOS 5.1。

MyViewController.h MyViewController.h

#import <UIKit/UIKit.h>
#import "DataHelper.h"

@interface MyViewController : UIViewController
{
    DataHelper *dataHelper;
    NSMutableArray *recentPosts;
    NSMutableArray *popularPosts;
}

@property (nonatomic, strong) DataHelper *dataHelper;
@property (nonatomic, strong) NSMutableArray *recentPosts;
@property (nonatomic, strong) NSMutableArray *popularPosts;

@end

viewDidLoad in MyViewController.m MyViewController.m中的viewDidLoad

@implementation MyViewController

@synthesize dataHelper, recentPosts, popularPosts;

- (void)viewDidLoad
{
    [super viewDidLoad];

    recentPosts = [[NSMutableArray alloc] init];
    popularPosts = [[NSMutableArray alloc] init];
    dataHelper = [[DataHelper alloc] init];

    // I've also tried initializing the ivars as above, with "self." prefix

    recentPosts = [dataHelper getSuggestionsByMostRecent:1];
    popularPosts = [dataHelper getSuggestionsByPopular:1];
}

ARC expects your method names to follow the cocoa(-touch) naming conventions . ARC希望您的方法名称遵循可可(-touch)命名约定 and getSuggestionsByMostRecent violates that convention. 并且getSuggestionsByMostRecent违反了该约定。 Especially this one: 特别是这个:

Use “get” only for methods that return objects and values indirectly. 仅将“ get”用于间接返回对象和值的方法。 You should use this form for methods only when multiple items need to be returned. 仅当需要返回多个项目时,才应将此表格用于方法。

that would be a valid get-method (not a good one though): - (void)getString:(NSString **)str 那将是一种有效的获取方法(虽然不是一个好方法- (void)getString:(NSString **)str

Without using correct naming scheme ARC does not know where to add retain or releases. 如果不使用正确的命名方案,ARC将不知道在何处添加保留或发布。

Try to rename your method to something like suggestionsSortedByMostRecent: . 尝试将您的方法重命名为RecommendationsSortedByMostRecent suggestionsSortedByMostRecent: Without the get. 没有得到。

I would synthesize like this: 我将这样合成:

@synthesize dataHelper = _dataHelper;
// same for the other two here

and use _dataHelper only. 并仅使用_dataHelper。

And for adding the posts, didn't you want to do this instead: 对于添加帖子,您是否不想这样做:

[_recentPosts addObjectsFromArray:[_dataHelper getSuggestionsByMostRecent:1]];

XCode中的LLDB存在内存读取问题(4.3.1似乎也受到影响),因此我建议您使用GDB进行调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM