0

Let's say I have an NSInteger set up in Class1 as so:

@property (nonatomic) NSInteger *amount;

In a method in Class1 I add 5 to amount, like so:

amount += 5;

Now in Class2, I want to be able to get the value of amount and display it in an NSLog. What is a way to do this? I tried:

shop = [[Shop alloc]init];
NSLog(@"%i", shop.amount);

but that doesn't work because it creates a new instance of shop every time I want to display amount, and I don't know how to fix that.

I can declare a global variable but I would like to stay away from those, so what other solutions are there?

Thanks.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
SaleenS7
  • 402
  • 4
  • 15
  • 1
    It looks like you know how to solve that problem, Shop should be a property of `Class2` that you initialize once. Unless I misunderstood something ? – meda Sep 07 '14 at 23:37
  • Yeah I know what I need to do but I'm not sure how to exactly do it. When I'm setting Shop as a property of `Class2` I used `@property (strong, nonatomic) Shop *shop;` then initialized shop in the init method as so: `shop = [[Shop alloc]init];` and then I just call the NSLog like this: `NSLog(@"%i", shop.amount);`. I don't see what is wrong with this code. When I run the program it still tells me that the amount is 0 after clicking the button a few times where amount should not be 0 anymore. – SaleenS7 Sep 07 '14 at 23:52
  • You have to refer to the instance where you set the value. Requires thought. Values do not magically propagate between instances of the class. – Hot Licks Sep 08 '14 at 04:10
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Hot Licks Sep 08 '14 at 04:13

1 Answers1

0

Replace:

@property (nonatomic) NSInteger *amount;

with

@property (strong, nonatomic) Shop *shop;

Then access the shop object after initialization

shop.amount = 0;

NSLog(@amount  = "%i", shop.amount);

shop.amount += 5;

NSLog(@"amount incremented by 5 = %i", shop.amount);
meda
  • 45,103
  • 14
  • 92
  • 122
  • Thanks that works within Class2, however how would I access that variable in Class1 (Shop)? Sorry, in my comment I made it unclear by calling Class1 Shop. I know I need to get the shop object on Class1 but how do I do that without setting a new property? – SaleenS7 Sep 08 '14 at 10:49
  • @user3803430 then you would pass `shop` object back to that class very easy, for example `class1.shopObject = shop` – meda Sep 08 '14 at 13:33
  • Hold on I think I confused both of us with my question. Referring back to the original post, I defined `@property (nonatomic) NSInteger *amount;`. If I replace that with `@property (strong, nonatomic) Shop *shop;` then there is no amount variable anymore. So I can't change a variable that doesn't exist. Also the newly defined property `@property (strong, nonatomic) Shop *shop;` is in the objective-c class Shop, so then isn't it just referring to itself? Sorry if these are odd or easy questions, I need to reread some info on objects – SaleenS7 Sep 09 '14 at 01:41
  • You getting confused because you need to think MVC. Shop is the model, it holds the properties, if you add each property to class1 then you defeat the purpose of MVC. Do you have a sample project to show me? Maybe i can fix it and give it back to u sp you see how i did things? – meda Sep 09 '14 at 01:51
  • Ahh okay. Sure that would be great! I actually did manage to fix the issue using NSUserDefaults, which I know isn't supposed to be used for passing data, but I do need to store it in NSUserDefaults anyway because I need the data to be saved. But I'm still interested in using properties. Here is a sample project, I uploaded it to Uppit. http://up.ht/1pJCQqM Thank you very much. – SaleenS7 Sep 09 '14 at 02:14
  • Kk please give me a brief idea of what you are trying to achieve – meda Sep 09 '14 at 02:18
  • User clicks the +1 button (which is in Shop), 1 is added to `NSInteger amount`. User clicks "check" button (which is in MainScene), display value of `amount` in an NSLog. – SaleenS7 Sep 09 '14 at 10:34
  • I don't want to be annoying and pushy but can you do the sample program? I've been searching around and I still can't understand how to call a method on an existing instance of a different class. If not, can you point me to some tutorial/thread that provides sample code or at least explains how to do it? Thanks – SaleenS7 Sep 12 '14 at 02:30
  • Sorry I wanted to get back to you but I got caught up with things – meda Sep 12 '14 at 02:31
  • Are you available for chat @user3803430 – meda Sep 12 '14 at 02:31
  • No worries, take your time. I will be available today at the time that you posted yesterday – SaleenS7 Sep 12 '14 at 10:31
  • I'm ready to chat. I'll be on for the next five hours so feel free to start up a chat whenever you are least busy. Thanks so much again @meda – SaleenS7 Sep 12 '14 at 22:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61153/discussion-between-meda-and-user3803430). – meda Sep 13 '14 at 00:46