简体   繁体   中英

objective-C syntax beginner question

what is the syntax to use an object (NSString) that declared in another class?

object workId in class works , i want to use it's value in class jobs .

thanks.

Go here: 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]

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'.

@property(nonatomic, copy) NSString* theString;

If Jobs has a pointer to Works like so:

@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.

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:

#import <Foundation/Foundation.h>

@interface Person : NSObject {
    NSString * name;
}
@end

in Person.m:

@implementation Person

- (NSString*) name {
    return name;
}

- (void)setName:(NSString *)aName {
    [name autorelease];
    name = [aName copy];
}

@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM