Title: Access int from Another Objective-C Class
Tags: ios;objective-c;iphone;xcode;int
Question: I've trying to get an int from another class and it just says that the number is 0 (but it's not).
This is in Class2.h:
@interface Class2 : SKScene <SKPhysicsContactDelegate&gt;{
Class1 * class;
This is in Class2.m:
NSLog(@"%i", class.thisint);
This is in Class1.h:
@property (assign, nonatomic) int thisint;
This is in Class1.m (in viewDidLoad):
thisint = 5;
The NSLog is being called well after the viewDidLoad method but it just keeps saying 0. How do I get this int from Class1? I don't know if the fact that Class2 is an SKScene affects this...
Comment: try using the accessor to assign thisint? self.thisint = 5; I would also remove (assign, nonatomic) for primitives and just declare the property like @property int thisint; Also, where are you alloc and init-ing the ivar named 'class' in Class2 ? All you have thus far is a nil pointer to Class1 named 'class'


Here is another answer: class is an existing method on NSObject and I would hope that you've just used that name for the purposes of this question - if not, please change the name of the variable, it will only lead to confusion.
What do you see if you log class:
NSLog(@"%@",class);
(Put that next to where your existing log is)
How and where are you assigning to the class variable?
A value of 0 probably means one of two things:
class is nil. This means you haven't assigned it anywhere. You don't magically get a value in a property just because you've declared one
class is a different instance to the one you think it is. This is a common beginner mistake, where you've done something like
class = [[Class2 alloc] init];
Which creates a new instance. You need to get a reference to the existing instance, which I can't tell you how to do without seeing more code.
Comment for this answer: Yeah I used class just for the example. The original was game. When I do NSLog(@"%@",class); it says (null). This is the only Class1 code I have in Class2 (in the .h): Class1 * class;
I didn't make a new instance because I knew it wouldn't work and I basically need to know to to make a reference to it.
Comment for this answer: Well you either need an outlet, if you're using IB, or wherever you create the thing you need to pass it along. Like I said, it's hard to tell without more details.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:09/13/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ios><objective-c><iphone><xcode><int>', 'Title': 'Access int from Another Objective-C Class', 'ViewCount': 36, 'id': 25820553, 'user_AboutMe': '', 'user_AccountId': 4987293.0, 'user_DisplayName': 'Kyle', 'user_Reputation': 45.0, 'user_Views': 1.0, 'user_id': 4010496.0}}