Why I can't access NSMutable Array in my method?












2















I try to add object to my NSMutable array in my method, but keep getting error. It works, if I add the object in init. It doesn't say that anything is wrong until I try to execute the code.



This is below the #import stuff where I declare two arrays:



NSMutableArray *actions1, *actions2;


This is in init:



actions1 = [NSMutableArray array];


Here I try to add 1 to the array:



- (void) storeAction:(int) action {
[actions1 addObject:@"1"];
}


The same code works in int as I said earlier.



I also would like it to store the int value declared "action", but this didn't seem to work either.



[addObject:@"%d", action];









share|improve this question























  • Regarding the [addObject:@"%d", action]; - please elaborate what does it mean "didn't seem to work either".. what's happening ? and BTW - why aren't you storing the int as a NSNumber object ? ([NSNumber numberWithInt:1])

    – Guys
    Feb 29 '12 at 22:26


















2















I try to add object to my NSMutable array in my method, but keep getting error. It works, if I add the object in init. It doesn't say that anything is wrong until I try to execute the code.



This is below the #import stuff where I declare two arrays:



NSMutableArray *actions1, *actions2;


This is in init:



actions1 = [NSMutableArray array];


Here I try to add 1 to the array:



- (void) storeAction:(int) action {
[actions1 addObject:@"1"];
}


The same code works in int as I said earlier.



I also would like it to store the int value declared "action", but this didn't seem to work either.



[addObject:@"%d", action];









share|improve this question























  • Regarding the [addObject:@"%d", action]; - please elaborate what does it mean "didn't seem to work either".. what's happening ? and BTW - why aren't you storing the int as a NSNumber object ? ([NSNumber numberWithInt:1])

    – Guys
    Feb 29 '12 at 22:26
















2












2








2








I try to add object to my NSMutable array in my method, but keep getting error. It works, if I add the object in init. It doesn't say that anything is wrong until I try to execute the code.



This is below the #import stuff where I declare two arrays:



NSMutableArray *actions1, *actions2;


This is in init:



actions1 = [NSMutableArray array];


Here I try to add 1 to the array:



- (void) storeAction:(int) action {
[actions1 addObject:@"1"];
}


The same code works in int as I said earlier.



I also would like it to store the int value declared "action", but this didn't seem to work either.



[addObject:@"%d", action];









share|improve this question














I try to add object to my NSMutable array in my method, but keep getting error. It works, if I add the object in init. It doesn't say that anything is wrong until I try to execute the code.



This is below the #import stuff where I declare two arrays:



NSMutableArray *actions1, *actions2;


This is in init:



actions1 = [NSMutableArray array];


Here I try to add 1 to the array:



- (void) storeAction:(int) action {
[actions1 addObject:@"1"];
}


The same code works in int as I said earlier.



I also would like it to store the int value declared "action", but this didn't seem to work either.



[addObject:@"%d", action];






iphone objective-c methods






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 29 '12 at 20:49









LiuhuLiuhu

4118




4118













  • Regarding the [addObject:@"%d", action]; - please elaborate what does it mean "didn't seem to work either".. what's happening ? and BTW - why aren't you storing the int as a NSNumber object ? ([NSNumber numberWithInt:1])

    – Guys
    Feb 29 '12 at 22:26





















  • Regarding the [addObject:@"%d", action]; - please elaborate what does it mean "didn't seem to work either".. what's happening ? and BTW - why aren't you storing the int as a NSNumber object ? ([NSNumber numberWithInt:1])

    – Guys
    Feb 29 '12 at 22:26



















Regarding the [addObject:@"%d", action]; - please elaborate what does it mean "didn't seem to work either".. what's happening ? and BTW - why aren't you storing the int as a NSNumber object ? ([NSNumber numberWithInt:1])

– Guys
Feb 29 '12 at 22:26







Regarding the [addObject:@"%d", action]; - please elaborate what does it mean "didn't seem to work either".. what's happening ? and BTW - why aren't you storing the int as a NSNumber object ? ([NSNumber numberWithInt:1])

– Guys
Feb 29 '12 at 22:26














3 Answers
3






active

oldest

votes


















5














[NSMutableArray array]; is returning an autoreleased object, by the time you try to access it, it is most likely deallocated already. Try [[NSMutableArray alloc] init]; instead. And than you should urgently check the memory management rules.






share|improve this answer


























  • I prefer this to [[NSMutableArray array] retain], plus you explained why your solution works. +1

    – Daniel G. Wilson
    Feb 29 '12 at 20:55











  • Thanks! Will read that tomorrow. Any idea why this won't work? [addObject:@"%d", action];

    – Liuhu
    Feb 29 '12 at 22:06








  • 1





    addObject takes a string and doesn't do formatting. You want: [array addObject:[NSString stringWithFormat:@"%d", action]];

    – gregheo
    Feb 29 '12 at 23:00











  • Thank you. Love peple who are willing to help.

    – Liuhu
    Mar 1 '12 at 14:40



















2














Try out this code



actions1 = [[NSMutableArray alloc] init];


Hope this helps.






share|improve this answer

































    0














    Alternatively, in your header file:



    @property(nonatomic, strong)NSMutableArray *actions1;


    Then in the implantation file:



    @synthesize actions1 = _actions1;


    Then you can access your array as self.actions1.






    share|improve this answer


























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9506398%2fwhy-i-cant-access-nsmutable-array-in-my-method%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      5














      [NSMutableArray array]; is returning an autoreleased object, by the time you try to access it, it is most likely deallocated already. Try [[NSMutableArray alloc] init]; instead. And than you should urgently check the memory management rules.






      share|improve this answer


























      • I prefer this to [[NSMutableArray array] retain], plus you explained why your solution works. +1

        – Daniel G. Wilson
        Feb 29 '12 at 20:55











      • Thanks! Will read that tomorrow. Any idea why this won't work? [addObject:@"%d", action];

        – Liuhu
        Feb 29 '12 at 22:06








      • 1





        addObject takes a string and doesn't do formatting. You want: [array addObject:[NSString stringWithFormat:@"%d", action]];

        – gregheo
        Feb 29 '12 at 23:00











      • Thank you. Love peple who are willing to help.

        – Liuhu
        Mar 1 '12 at 14:40
















      5














      [NSMutableArray array]; is returning an autoreleased object, by the time you try to access it, it is most likely deallocated already. Try [[NSMutableArray alloc] init]; instead. And than you should urgently check the memory management rules.






      share|improve this answer


























      • I prefer this to [[NSMutableArray array] retain], plus you explained why your solution works. +1

        – Daniel G. Wilson
        Feb 29 '12 at 20:55











      • Thanks! Will read that tomorrow. Any idea why this won't work? [addObject:@"%d", action];

        – Liuhu
        Feb 29 '12 at 22:06








      • 1





        addObject takes a string and doesn't do formatting. You want: [array addObject:[NSString stringWithFormat:@"%d", action]];

        – gregheo
        Feb 29 '12 at 23:00











      • Thank you. Love peple who are willing to help.

        – Liuhu
        Mar 1 '12 at 14:40














      5












      5








      5







      [NSMutableArray array]; is returning an autoreleased object, by the time you try to access it, it is most likely deallocated already. Try [[NSMutableArray alloc] init]; instead. And than you should urgently check the memory management rules.






      share|improve this answer















      [NSMutableArray array]; is returning an autoreleased object, by the time you try to access it, it is most likely deallocated already. Try [[NSMutableArray alloc] init]; instead. And than you should urgently check the memory management rules.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Feb 29 '12 at 20:59

























      answered Feb 29 '12 at 20:52









      vikingosegundovikingosegundo

      48.7k14116163




      48.7k14116163













      • I prefer this to [[NSMutableArray array] retain], plus you explained why your solution works. +1

        – Daniel G. Wilson
        Feb 29 '12 at 20:55











      • Thanks! Will read that tomorrow. Any idea why this won't work? [addObject:@"%d", action];

        – Liuhu
        Feb 29 '12 at 22:06








      • 1





        addObject takes a string and doesn't do formatting. You want: [array addObject:[NSString stringWithFormat:@"%d", action]];

        – gregheo
        Feb 29 '12 at 23:00











      • Thank you. Love peple who are willing to help.

        – Liuhu
        Mar 1 '12 at 14:40



















      • I prefer this to [[NSMutableArray array] retain], plus you explained why your solution works. +1

        – Daniel G. Wilson
        Feb 29 '12 at 20:55











      • Thanks! Will read that tomorrow. Any idea why this won't work? [addObject:@"%d", action];

        – Liuhu
        Feb 29 '12 at 22:06








      • 1





        addObject takes a string and doesn't do formatting. You want: [array addObject:[NSString stringWithFormat:@"%d", action]];

        – gregheo
        Feb 29 '12 at 23:00











      • Thank you. Love peple who are willing to help.

        – Liuhu
        Mar 1 '12 at 14:40

















      I prefer this to [[NSMutableArray array] retain], plus you explained why your solution works. +1

      – Daniel G. Wilson
      Feb 29 '12 at 20:55





      I prefer this to [[NSMutableArray array] retain], plus you explained why your solution works. +1

      – Daniel G. Wilson
      Feb 29 '12 at 20:55













      Thanks! Will read that tomorrow. Any idea why this won't work? [addObject:@"%d", action];

      – Liuhu
      Feb 29 '12 at 22:06







      Thanks! Will read that tomorrow. Any idea why this won't work? [addObject:@"%d", action];

      – Liuhu
      Feb 29 '12 at 22:06






      1




      1





      addObject takes a string and doesn't do formatting. You want: [array addObject:[NSString stringWithFormat:@"%d", action]];

      – gregheo
      Feb 29 '12 at 23:00





      addObject takes a string and doesn't do formatting. You want: [array addObject:[NSString stringWithFormat:@"%d", action]];

      – gregheo
      Feb 29 '12 at 23:00













      Thank you. Love peple who are willing to help.

      – Liuhu
      Mar 1 '12 at 14:40





      Thank you. Love peple who are willing to help.

      – Liuhu
      Mar 1 '12 at 14:40













      2














      Try out this code



      actions1 = [[NSMutableArray alloc] init];


      Hope this helps.






      share|improve this answer






























        2














        Try out this code



        actions1 = [[NSMutableArray alloc] init];


        Hope this helps.






        share|improve this answer




























          2












          2








          2







          Try out this code



          actions1 = [[NSMutableArray alloc] init];


          Hope this helps.






          share|improve this answer















          Try out this code



          actions1 = [[NSMutableArray alloc] init];


          Hope this helps.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 24 '12 at 19:50









          Parth Bhatt

          14.2k25121212




          14.2k25121212










          answered Feb 29 '12 at 20:53









          VinnieVinnie

          1,3771013




          1,3771013























              0














              Alternatively, in your header file:



              @property(nonatomic, strong)NSMutableArray *actions1;


              Then in the implantation file:



              @synthesize actions1 = _actions1;


              Then you can access your array as self.actions1.






              share|improve this answer






























                0














                Alternatively, in your header file:



                @property(nonatomic, strong)NSMutableArray *actions1;


                Then in the implantation file:



                @synthesize actions1 = _actions1;


                Then you can access your array as self.actions1.






                share|improve this answer




























                  0












                  0








                  0







                  Alternatively, in your header file:



                  @property(nonatomic, strong)NSMutableArray *actions1;


                  Then in the implantation file:



                  @synthesize actions1 = _actions1;


                  Then you can access your array as self.actions1.






                  share|improve this answer















                  Alternatively, in your header file:



                  @property(nonatomic, strong)NSMutableArray *actions1;


                  Then in the implantation file:



                  @synthesize actions1 = _actions1;


                  Then you can access your array as self.actions1.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 22 '18 at 1:49









                  Pang

                  6,9881666105




                  6,9881666105










                  answered Feb 29 '12 at 22:06









                  dbrajkovicdbrajkovic

                  3,41511314




                  3,41511314






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9506398%2fwhy-i-cant-access-nsmutable-array-in-my-method%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to change which sound is reproduced for terminal bell?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?