[英]objective-C syntax beginner question
what is the syntax to use an object (NSString) that declared in another class? 使用在另一个类中声明的对象(NSString)的语法是什么?
object workId
in class works
, i want to use it's value in class jobs
. 对象
workId
在课堂上works
,我想在课堂jobs
使用它的价值。
thanks. 谢谢。
Go here: http://www.cocoadevcentral.com/d/learn_objectivec/ 请访问: http : //www.cocoadevcentral.com/d/learn_objectivec/
And scroll down to the "Properties" section. 然后向下滚动到“属性”部分。
if you declared workId as a property and synthesized it, you should be able to access it using works.workId or [works workId] 如果你将workId声明为属性并合成它,你应该能够使用works.workId或[works workId]访问它
http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html
If you'd like to hold a pointer to the same object you can declare a second property in the Jobs class using 'assign' or 'retain', if you'd just like a copy you could declare the property using 'copy'. 如果你想持有指向同一个对象的指针,你可以使用'assign'或'retain'在Jobs类中声明第二个属性,如果你只是喜欢一个副本,你可以使用'copy'来声明属性。
@property(nonatomic, copy) NSString* theString;
If Jobs has a pointer to Works like so: 如果Jobs有一个指向Works的指针,那么:
@interface Jobs
{
Works* works;
}
@property (nonatomic, retain) Works* works;
@end
You could just use self.works.workId to access the work id from within an instance of the Jobs class. 您可以使用self.works.workId从Jobs类的实例中访问工作ID。
Could you let us know a little more about your particular use case, it would help to determine what you should be doing. 您能否让我们更多地了解您的特定用例,这将有助于确定您应该做什么。
in Person.h: 在Person.h中:
#import <Foundation/Foundation.h>
@interface Person : NSObject {
NSString * name;
}
@end
in Person.m: 在Person.m中:
@implementation Person
- (NSString*) name {
return name;
}
- (void)setName:(NSString *)aName {
[name autorelease];
name = [aName copy];
}
@end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.