XCode 中的目标 C,设置和获取存储在 NSArray 中的类对象中的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10525146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:15:37  来源:igfitidea点击:

Objective C in XCode, set and get variables in class object which is stored in NSArray

objective-cxcodeclassobject

提问by Piotr

I am trying to write my own class storing data about libraries and then store each object in an NSArray.

我正在尝试编写自己的类来存储有关库的数据,然后将每个对象存储在 NSArray 中。

My interface:

我的界面:

#import <Foundation/Foundation.h>

@interface LibraryInfo : NSObject
{

NSString* name;
NSString* address;
NSString* link;
NSString* coordinates;

}

- (NSString*) getName;
- (NSString*) getAddress;
- (NSString*) getLink;
- (NSString*) getCoordinates;

- (void) setName: (NSString*)input;
- (void) setAddress: (NSString*)input;
- (void) setLink: (NSString*)input;
- (void) setCoordinates: (NSString*)input;

@end

My implementation:

我的实现:

#import "LibraryInfo.h"

@implementation LibraryInfo

- (void) setName: (NSString*)input
{
name = input;
}

- (void) setAddress: (NSString*)input
{
address = input;
}

- (void) setLink: (NSString*)input
{
link = input;
}

- (void) setCoordinates: (NSString*)input
{
coordinates = input;
}

- (NSString*) getName {
return name;
}

- (NSString*) getAddress {
return address;
}

- (NSString*) getLink {
return link;
}

- (NSString*) getCoordinates {
return coordinates;
}

@end

I write and store each object in the NSArray, like so:

我将每个对象写入并存储在 NSArray 中,如下所示:

LibraryInfo *library = [[LibraryInfo alloc] init];
[library setName:( ( name != nil && name.length > 0 ) ? name : @"No Name" )];
[library setAddress:( ( address != nil && address.length > 0 ) ? address : @"No Address" )];
[library setLink:( ( link != nil && link.length > 0 ) ? link : @"No Link" )];
[library setCoordinates:( ( coordinate != nil && coordinate.length > 0 ) ? coordinate : @"No Coordinates" )];

[libraries addObject: library];

Where 'libraries' is an NSArray.

其中“库”是一个 NSArray。

Then I try to read the values like so:

然后我尝试像这样读取值:

cell.textLabel.text = [[ libraries objectAtIndex:indexPath.row ] getName ];
cell.detailTextLabel.text = [[ libraries objectAtIndex:indexPath.row ] getAddress ];

I think it may be something to do with the setting of the values, or maybe they are not properly stored or retained, but I get the following error and a marker by my 'setName' method
when the App 'crashes' and pauses:

我认为这可能与值的设置有关,或者它们没有正确存储或保留,但是
当应用程序“崩溃”并暂停时,我的“setName”方法出现以下错误和标记:

self    LibraryInfo *const  0x068a1760
input   NSString *  0x068a3550
name    NSString *  0x00000000

Thanks for any advice as how to fix this... I'm using XCode 4.3 so no releases/retains needed.

感谢您提供有关如何解决此问题的任何建议...我使用的是 XCode 4.3,因此不需要发布/保留。

Take care! :)

小心!:)

Sincerely, Piotr.

真诚的,彼得。

回答by Ryan Poolos

You need to declare properties. IVars aren't going to store your data the way you're expecting

您需要声明属性。IVars 不会以您期望的方式存储您的数据

LibraryInfo.h

图书馆信息.h

@interface LibraryInfo : NSObject

@property (nonatomic, strong) NSString* name;
@property (nonatomic, strong) NSString* address;
@property (nonatomic, strong) NSString* link;
@property (nonatomic, strong) NSString* coordinates;

LibraryInfo.m

图书馆信息.m

@implementation LibraryInfo
@synthesize name;
@synthesize address;
@synthesize link;
@synthesize coordinates;

Then creating libraries would be simple

那么创建库就很简单了

LibraryInfo *library = [[LibraryInfo alloc] init];
library.name = (name.length > 0 ? name : @"No Name");
library.address = (address.length > 0 ? address : @"No Address")
library.link = (link.length > 0 ? link : @"No Link")
library.coordinates = (coordinates.length > 0 ? coordinates : @"No Coordinates")

Then in your TableView cell.

然后在您的 TableView 单元格中。

cell.textLabel.text = [libraries objectAtIndex:indexPath.row].name;
cell.detailTextLabel.text = [libraries objectAtIndex:indexPath.row].address;