-1

I would like to know how to call a method from a NSObjectClass that returns a NSMutableArray and save that returning MutableArray into a NSMutableArray in the calling class.

effectively this is how it looks

NSObject class has the method with the return value

- (NSMutableArray *)myMethod {
  // do some stuff

  return myMutableArray
}

The class that calls this method dose all of the initialization so I can use the myMethod method but what I am uncertian of is how to get the returning myMutableArray into a MutableArray in this class...

is it something like

TheNameOfTheNSObjectClass *myObjClass = [[TheNameOfTheObjectClass alloc] init];
anotherMutableArray = [myObjClass myMethod];

because thats what I am trying at the moment and I am getting a warning

Incompatible pointer types assigning to 'NSMutableArray *__strong' from 'TheNameOfTheNSObjectClass *'

any help would be appreciated.

Code cracker
  • 3,105
  • 6
  • 37
  • 67
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183
  • I think more info is needed. Can you add the interface for `TheNameOfTheNSObjectClass` (elide unreferenced methods) and the declaration of `anotherMutableArray`? ALso what do you mean by "into a `NSMutableArray`"? Do you wish to copy the returned array or just reference it? – CRD Apr 08 '13 at 20:05
  • You are going to have to post more code, are you using ARC? How are these two NSMutableArray's defined? – HackyStack Apr 08 '13 at 20:05
  • 1
    I think your first step should be to learn programming, specifically the concepts of pointers, objects, and imperative programs. – Hot Licks Apr 08 '13 at 20:14
  • all I need to know is how to call the method so the returning value is put into the object... – HurkNburkS Apr 08 '13 at 20:15
  • 1
    As written, the question doesn't make any sense. – bbum Apr 08 '13 at 20:30
  • How is `anotherMutableArray` declared? – Chuck Apr 08 '13 at 20:41
  • @Chuck **NSMutableArray *anotherMutableArray = [[NSMutableArray alloc] init];** – HurkNburkS Apr 08 '13 at 20:51
  • @HotLicks if you looked at the code example of mine you would see I know how to return a value by the decliration of my method return type and the use of the return function in my method. **- (this is the return type *)methodname {** then in the fuction you use **return myMethodthatisOfTypeReturnType** lol. maybe it was you who fell asleep and dreampt that I did. – HurkNburkS Apr 08 '13 at 20:53
  • You're instantiating an array only to immediately assign *a different instance* to the variable, losing the original instantiation, that is a leak (non-ARC) or pointless (ARC). And it still doesn't make the question any clearer. Show the line of code that generates that error message (and the declaration of all types on the line). – bbum Apr 08 '13 at 20:59
  • Well, you have the assignment you need in your post above (though you shouldn't be initializing anotherMutableArray as you are a few comments up). So what's your problem?? – Hot Licks Apr 08 '13 at 21:01
  • @HotLicks how do you pass the returning MutableArray from the method into the MutablerArray I just made.. That is what I am wanting to know.. Its just I am not to sure what to do here... – HurkNburkS Apr 08 '13 at 21:11
  • 1
    You need to understand the difference between an object and a pointer. Also likely you need to understand the difference between a value and a variable/parameter. – Hot Licks Apr 08 '13 at 21:15
  • @bbum I am using ARC, Effectivly I am getting a bunch of data from a server request, I am then using the NSObject Class to parse the data into their correct types for instance NSNumbers, BOOLS and NSStrings as currently everything is NSData. I then save the data into a NSMutableArray of Dictionaries that I am returning. The last thing I need to do is pass the returning value into a NSMUtableArray I can use. Why is this not the correct thing to do? – HurkNburkS Apr 08 '13 at 21:19
  • @HotLicks I understand the difference between all those things. – HurkNburkS Apr 08 '13 at 21:25
  • Show us the line that produces this message: Incompatible pointer types assigning to 'NSMutableArray *__strong' from 'TheNameOfTheNSObjectClass *' So far you haven't done this. – Hot Licks Apr 08 '13 at 21:25
  • @HotLicks that happens on this line **anotherMutableArray = [myObjClass myMethod];** – HurkNburkS Apr 08 '13 at 21:26
  • 1
    BS. myMethod returns an NSMutableArray. (At least according to what you've claimed above.) The error message says you're assigning a TheNameOfTheNSObjectClass pointer to an NSMutableArray pointer. Where is it that you're doing that?? – Hot Licks Apr 08 '13 at 21:28
  • And don't use "myObjClass" for the name of an instance! – Hot Licks Apr 08 '13 at 21:30
  • yes I know its just an example. – HurkNburkS Apr 08 '13 at 21:42
  • 1
    That appears to be the issue; in your efforts to create "just an example" for SO question purposes, you lost the critical pieces of signal that would allow us to efficiently answer your question. Best to copy/paste the code directly, if at all possible. – bbum Apr 08 '13 at 21:43
  • yea.. sorry I was just trying to keep it simple because I use alot of weird keywords that relate to the data I am calling so it probably would have made even less sense.. I have learnt my leasson though so will do that next time for sure. – HurkNburkS Apr 08 '13 at 22:04

1 Answers1

2

If you have a method like the following on a class called Baginator:

- (NSMutableArray *) mutableDoggyBags;

And you want to call that method then:

Baginator *b = [Baginator new];
NSMutableArray *bags;

bags = [b mutableDoggyBags];

Parsing the question, it sounds like the equivalent of mutableDoggyBags is declared to return something other than a mutable array.

Specifically, if this line is producing an error on the assignment:

anotherMutableArray = [myObjClass myMethod]; 

Then, the following must be true:

• `myMethod` is not returning the same type as the type of `anotherMutableArray`.

In your case, it sounds like myMethod is declared as returning an instance of TheNameOfTheNSObjectClass. I.e.:

- (TheNameOfTheNSObjectClass*) myMethod;
bbum
  • 162,346
  • 23
  • 271
  • 359
  • I have solved it you are correct initally it was returning TheNameOfTheNSObjectClass, I closed the app opened it then cleaned it with the clean build and it worked!!! WTF – HurkNburkS Apr 08 '13 at 22:32