Objective-C 基本类相关问题,使用类文件保留特定对象的值
成员、学者、代码大师。 我的背景远离任何计算机编程,因此我的问题对您来说可能看起来很基本并且有点微不足道。尽管如此,我似乎无法理解它。 我用谷歌搜索并寻找答案,只是让自己更加困惑。 因此,我恳请您提供一个适合像我这样的非技术人员以及其他类似的人的简单解释。
我在下面留下了一条评论,文字为“这就是问题”,指的是我的问题。
// character.h
#import <Foundation/Foundation.h>
@interface character : NSObject {
NSString *name;
int hitPoints;
int armorClass;
}
@property (nonatomic,retain) NSString *name;
@property int hitPoints,armorClass;
-(void)giveCharacterInfo;
@end
// character.m
#import "character.h"
@implementation character
@synthesize name,hitPoints,armorClass;
-(void)giveCharacterInfo{
NSLog(@"name:%@ HP:%i AC:%i",name,hitPoints,armorClass);
}
@end
// ClassAtLastViewController.h
#import <UIKit/UIKit.h>
@interface ClassAtLastViewController : UIViewController {
}
-(void)callAgain;
@end
// ClassAtLastViewController.m
#import "ClassAtLastViewController.h"
#import "character.h"
@implementation ClassAtLastViewController
- (void)viewDidLoad {
//[super viewDidLoad];
character *player = [[character alloc]init];
player.name = @"Minsc";
player.hitPoints = 140;
player.armorClass = 10;
[player giveCharacterInfo];
[player release];
// Up until here, All peachy!
[self performSelector:@selector(callAgain) withObject:nil afterDelay:2.0];
}
-(void)callAgain{
// Here is the issue, I assume that since I init the player again I loss everything
// Q1. I loss all the data I set above, where is it than?
// Q2. What is the proper way to implement this
character *player = [[character alloc]init];
[player giveCharacterInfo];
}
预先非常感谢,请记住,我的背景与鲑鱼育种比与计算机代码更相关,如果对您来说都一样,请尝试将您的答案降低到我的水平。
Members, scholars, code gurus.
My background is far from any computer programming thus my question may seems basic and somewhat trivial to you. Nevertheless it seems that I can't put my head around it.
I have googled and searched for the answer, just to get myself confused even more.
With that, I would kindly ask for a simple explanation suitable for a non technical person such as myself and for other alike arriving to this thread.
I have left a comment with the text "Here is the issue" below, referring to my question.
// character.h
#import <Foundation/Foundation.h>
@interface character : NSObject {
NSString *name;
int hitPoints;
int armorClass;
}
@property (nonatomic,retain) NSString *name;
@property int hitPoints,armorClass;
-(void)giveCharacterInfo;
@end
// character.m
#import "character.h"
@implementation character
@synthesize name,hitPoints,armorClass;
-(void)giveCharacterInfo{
NSLog(@"name:%@ HP:%i AC:%i",name,hitPoints,armorClass);
}
@end
// ClassAtLastViewController.h
#import <UIKit/UIKit.h>
@interface ClassAtLastViewController : UIViewController {
}
-(void)callAgain;
@end
// ClassAtLastViewController.m
#import "ClassAtLastViewController.h"
#import "character.h"
@implementation ClassAtLastViewController
- (void)viewDidLoad {
//[super viewDidLoad];
character *player = [[character alloc]init];
player.name = @"Minsc";
player.hitPoints = 140;
player.armorClass = 10;
[player giveCharacterInfo];
[player release];
// Up until here, All peachy!
[self performSelector:@selector(callAgain) withObject:nil afterDelay:2.0];
}
-(void)callAgain{
// Here is the issue, I assume that since I init the player again I loss everything
// Q1. I loss all the data I set above, where is it than?
// Q2. What is the proper way to implement this
character *player = [[character alloc]init];
[player giveCharacterInfo];
}
Many thanks in advance, Kindly remember that my background is more related to Salmons breeding than to computer code, try and lower your answer to my level if it's all the same to you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它已被释放(释放)。注意:
即使你没有释放它,你仍然无法从
callAgain
访问它,因为你在viewDidLoad
中声明了player
>。如果您希望它可用于 ClassAtLastViewController 实例中的所有方法,则应将其设为实例变量。我不知道你的使用细节,但我想你想要这样的东西:
It's been freed (released). Notice:
Even if you didn't release it, you still wouldn't be able to access it from
callAgain
, because you declareplayer
inviewDidLoad
. If you want it available to all methods in instances ofClassAtLastViewController
, you should make it an instance variable.I don't know the specifics of your usage, but I imagine you want something like this:
当您在 callAgain 函数中初始化角色时,您实际上是在创建角色的第二个实例。尽管 callAgain 中创建的角色与 viewDidLoad 中创建的角色具有相同的名称,但它是一个完全独立的变量。你在viewDidLoad中初始化的字符只存在于viewDidLoad函数的范围内。
另外,当您在 viewDidLoad 中调用 [player release] 时,您将从内存中删除该角色实例。
我认为您想要做的是创建一个可以在整个类中使用的实例变量:
When you init the character in your callAgain function, you are actually creating a second instance of character. Even though the character created in callAgain has the same name as the character you created in viewDidLoad, it is a completely separate variable. The character you init in viewDidLoad only exists in the scope of the viewDidLoad function.
Also, when you call [player release] in viewDidLoad, you are removing that character instance from memory.
I think what you want to do is to create an instance variable that can be used throughout your class: