Why I can't access NSMutable Array in my method?
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
add a comment |
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
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
add a comment |
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
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
iphone objective-c methods
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
[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.
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
add a comment |
Try out this code
actions1 = [[NSMutableArray alloc] init];
Hope this helps.
add a comment |
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
.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
[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.
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
add a comment |
[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.
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
add a comment |
[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.
[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.
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
add a comment |
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
add a comment |
Try out this code
actions1 = [[NSMutableArray alloc] init];
Hope this helps.
add a comment |
Try out this code
actions1 = [[NSMutableArray alloc] init];
Hope this helps.
add a comment |
Try out this code
actions1 = [[NSMutableArray alloc] init];
Hope this helps.
Try out this code
actions1 = [[NSMutableArray alloc] init];
Hope this helps.
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
add a comment |
add a comment |
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
.
add a comment |
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
.
add a comment |
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
.
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
.
edited Nov 22 '18 at 1:49
Pang
6,9881666105
6,9881666105
answered Feb 29 '12 at 22:06
dbrajkovicdbrajkovic
3,41511314
3,41511314
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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