iPhone Xcode 数组列表可访问

发布于 2024-12-15 07:33:28 字数 1611 浏览 0 评论 0原文

目前正在开发我的第一个本机 iPhone 应用程序,该应用程序将集成到现有的 .net 应用程序中,并将使用 webServices。然而,我认为我面临的问题只不过是我对 xcode 的理解。

我希望我能解释这一点...

好吧,在我的第一个视图中,在 viewDidLoad 中,我转到网络服务并返回我的项目列表(这些填充表格视图),这完全可以正常工作:

--- snippet ---

- (void)viewDidLoad {
//GET ALL ITEMS
    myArray = [[NSMutableArray alloc] init];    
    MyWebService *webService = [[MyWebService alloc] init]; 
    myArray = [webService getAllNewsFunction];
    [super viewDidLoad];

}

-- - 片段 ---

好吧,我现在已经填充了我的表格视图,并等待您选择您的项目(在本例中是新闻文章),我需要确定 selectedItem 以便使用文章的详细信息填充下一个视图。然而,在 didSelectRowAtIndexPath 方法中,我的数组列表似乎不再可访问。我现在非常困惑,因为如果我只是在当前视图中的 viewDidLoad 中创建一个项目列表而不使用我的网络服务,例如:

--- snippet ---

//  listOfItems = [[NSMutableArray alloc] init];
// [myArray addObject:@"Iceland"];
//  [listOfItems addObject:@"Greenland"];
//  [listOfItems addObject:@"Switzerland"];
//  [listOfItems addObject:@"Norway"];
//  [listOfItems addObject:@"New Zealand"];
//  [listOfItems addObject:@"Greece"];
//  [listOfItems addObject:@"Italy"];
//  [listOfItems addObject:@"Ireland"];

--- snippet ---

上面的内容可以在didSelectRowAtIndexPath,我可以填充详细视图。

您能帮我找出我面临的问题以及如何解决这个问题吗?我知道我已经向您提供了非常基础的知识,因此如果我需要提供更多信息,我很乐意这样做。

-- 更新 --

好的,根据您的回复,我更新了以下内容。

我现在已将 myArray 声明为属性

@property (nonatomic, retain) NSMutableArray  *myArray;

,并将该行更新为:

self.myArray = [webService getAllNewsFunction];

-- update --

但是我现在在 self.myArray 行上收到错误“程序收到信号:“SIGABRT””。

有什么想法吗?

再次感谢

Currently developing my first Native iPhone application, the application is going to be integrating within an existing .net application and will be using webServices. However the problem i am facing i believe is nothing more than my understanding of xcode.

I hope i can explain this...

OK within my first view, within the viewDidLoad i go off to the webservice and return my list of items (these populate the tableview) this works completely fine:

--- snippet ---

- (void)viewDidLoad {
//GET ALL ITEMS
    myArray = [[NSMutableArray alloc] init];    
    MyWebService *webService = [[MyWebService alloc] init]; 
    myArray = [webService getAllNewsFunction];
    [super viewDidLoad];

}

--- snippet ---

Ok so i now have my tableview populated and awaiting for you to select your item (in this case a news article) i need to determine the selectedItem in order to populate the next view with the details of the article. However it appears that within the didSelectRowAtIndexPath method my array list is no longer accessible. I am very confused at this point due to if i simply create a list of items within my viewDidLoad within the current view without using my webservice for example:

--- snippet ---

//  listOfItems = [[NSMutableArray alloc] init];
// [myArray addObject:@"Iceland"];
//  [listOfItems addObject:@"Greenland"];
//  [listOfItems addObject:@"Switzerland"];
//  [listOfItems addObject:@"Norway"];
//  [listOfItems addObject:@"New Zealand"];
//  [listOfItems addObject:@"Greece"];
//  [listOfItems addObject:@"Italy"];
//  [listOfItems addObject:@"Ireland"];

--- snippet ---

the above is accessible within didSelectRowAtIndexPath and i can populate the detailview.

Can you please help me pinpoint the problem i am facing and how this can be solved. I understand i have given you the very basics so if i need to provide more information i am happy to do so.

-- update --

ok based on your response i have update the following.

I have now declared myArray as a property

@property (nonatomic, retain) NSMutableArray  *myArray;

and also updated the line to:

self.myArray = [webService getAllNewsFunction];

-- update --

however i now receive an error "program received signal: "SIGABRT"" on the self.myArray line.

any ideas?

Thanks Again

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南烟 2024-12-22 07:33:28

如果您遵循命名约定,则方法 [webService getAllNewsFunction] 将返回一个自动释放的数组。因此,当您再次访问它时,它很可能已经被释放了。

如果您使用 @property 声明 myArray (您应该避免这些问题),那么您可以通过执行以下操作来解决此问题:

self.myArray = [webService getAllNewsFunction];

这行:

myArray = [[NSMutableArray alloc] init];

也是多余的,并且导致内存泄漏,因为您在分配/初始化的新 NSMutableArray 后立即将 myArray 的值重新分配给 [webService getAllNewsFunction] d

更新

因此,通过阅读您的更新,我认为您需要首先查看您的项目现在的警告,其中可能会显示如下内容:

属性“myArray”需要定义方法“setMyArray” - 使用@synthesize、@dynamic 或在...中提供方法实现

问题的下一个线索出现在控制台中我得到类似的内容

-[TestAppDelegate setMyArray:]:无法识别的选择器发送到实例...

因此,归根结底,您要么需要提供方法

- (NSMutableArray *)myArray;
- (void)setMyArray:(NSMutableArray *)myArray;

,要么让编译器通过添加 @synthesize 来为您完成此操作像这样的语句

.m
@implementation YourClass

@synthesize myArray;

// The rest of your class methods

- (void)dealloc
{
    // release other ivars
    [myArray release];
    [super dealloc];
}

综合是更容易、更快的选项,编译器将根据您在 @property 声明中使用的选项来安排正确的内存管理。

If you are following naming conventions then the method [webService getAllNewsFunction] will return an autoreleased array. Therefore when you come to access it again it will most likely have been released already.

If you have used a @property to declare myArray (which you should to save yourself from these problems) then you can resolve this by doing:

self.myArray = [webService getAllNewsFunction];

This line:

myArray = [[NSMutableArray alloc] init];

is also superflous and causing a memory leak as you are reassigning the value of myArray to [webService getAllNewsFunction] immediately after without releasing the new NSMutableArray you alloc/init'd

UPDATE

So from reading your update I think you need to firstly look at the warnings your project now has which will probably read something like:

Property 'myArray' requires method 'setMyArray' to be defined - use @synthesize, @dynamic or provide a method implementation in ...

The next clue to the problem appears in the console I get something like this

-[TestAppDelegate setMyArray:]: unrecognized selector sent to instance ...

So what it all boils down to is you either need to provide the methods

- (NSMutableArray *)myArray;
- (void)setMyArray:(NSMutableArray *)myArray;

or let the compiler do it for you by adding a @synthesize statement like this

.m
@implementation YourClass

@synthesize myArray;

// The rest of your class methods

- (void)dealloc
{
    // release other ivars
    [myArray release];
    [super dealloc];
}

The synthesize is the easier, quicker options and the compiler will arrange for the coorect memory management depending on which options you use in your @property declaration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文