iphone - 无法在 iOS 中为对象设置值

标签 iphone ios objective-c

我是 iOS 开发的新手。

我有一门课叫宠物

@interface Pet : NSObject
@property(nonatomic, strong) NSString *petName;
@property(nonatomic, strong) NSString *petBreed;
@end

在我的一种方法中,我试图为 Pet 类型的声明对象设置值:
Pet *selectedPet;

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{        
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
NSString *currentPetTemp,*currentBreedTemp;
currentPetTemp = cell.textLabel.text;
currentBreedTemp = cell.detailTextLabel.text;
selectedPet.petName = currentPetTemp;
selectedPet.petBreed = currentBreedTemp;
NSLog(@"%@ Name1: %@",selectedPet.petName,currentPetTemp);

return indexPath;

}

NSLog 显示 'currentPetTemp' 的正确值,但 selectedPet.petName 显示 'null' .

任何帮助,将不胜感激。

最佳答案

您尚未对其进行初始化...执行以下操作:

Pet *selectedPet=[[Pet alloc]init];

或者,
 Pet *selectedPet=[Pet new];

编辑:

根据您的评论 Initializer element is not a compile time constant当您定义任何方法范围之外的变量时,会显示此警告。这个地方仅适用于常量值。

你需要在 viewDidLoad/init/awakeFromNib 中进行 alloc-init。

关于iphone - 无法在 iOS 中为对象设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14414802/

相关文章:

iphone - ios - 如何在 ios 中解析 HTML 内容?

ios - SKAudioNode 崩溃 : AVAudioPlayerNode. mm:333:开始:所需条件为假:_engine->IsRunning()

ios - 变量未在按钮按下操作内更新

ios - 从 cs193p 下载的 Photomania 应用程序显示一个空的 TableView

objective-c - drawRect 中的 UILabel 不绘制

iphone - 实现类似于 iPhone 联系人应用程序的 "add photo"按钮

ios - 自定义导航栏项目放置不当

ios - 快速进行 iOS 单元测试

ios - 如何在 nextViewController 字符串中存储 textField 值

iphone - 从 iPhone 应用程序上传到 YouTube 的视频没有声音