Object in array within array to string - objective-c

I have an array with arrays. I do:
predicateWithFormat:#"ANY SELF == %#",temphref];
NSArray *results = [shareArray filteredArrayUsingPredicate:predicate];
And if I print
(
(
"id=77",
"path=/cp012725.exe",
"share_type=3",
"token=88ae683504def4caab9154f1684dccdf"
) )
I would like to get these Array values into strings. stringForID stringForPath stringForShareType
stringForToken
I try to just NSString *stringForID = results[0][0];
But it fails when the array is empty, but array does not respond to if(results==nil)
if I print the resultswhen there are none, it prints ()so its not nil i guess.
Any suggestions?

The filter will always return an array, even if its empty, change your if statement from...
if(results==nil)
to this
if(results.count == 0)

Related

Getting object as singleArrayObject, how to convert in NSMutableArray?

in response, i am getting gas_cylinders key as single array object
"gas_cylinders":["[{\"quantity\":\"2\",\"name\":\"Medium Blue\",\"price\":\"100.0\",\"total_price\":\"200.0\"},{\"quantity\":\"3\",\"name\":\"Green\",\"price\":\"100.0\",\"total_price\":\"300.0\"},{\"quantity\":\"1\",\"name\":\"Dark Green\",\"price\":\"100.0\",\"total_price\":\"100.0\"}]"]
Note:- tripDictionary contain below data..
(lldb) po tripDictionary
{
"gas_cylinder_total" = 600;
"gas_cylinders" = (
"[{\"quantity\":\"2\",\"name\":\"Medium Blue\",\"price\":\"100.0\",\"total_price\":\"200.0\"},{\"quantity\":\"3\",\"name\":\"Green\",\"price\":\"100.0\",\"total_price\":\"300.0\"},{\"quantity\":\"1\",\"name\":\"Dark Green\",\"price\":\"100.0\",\"total_price\":\"100.0\"}]"
);
}
i am taking gas_cylinders like below way..
NSArray *arr = [tripDictionary valueForKey:#"gas_cylinders"];
if (arr && arr.count > 0) {
NSLog(#"first obj = %#",arr[0]);
}
output of above NSLog like..
first obj = [{"quantity":"2","name":"Medium Blue","price":"100.0","total_price":"200.0"},{"quantity":"3","name":"Green","price":"100.0","total_price":"300.0"},{"quantity":"1","name":"Dark Green","price":"100.0","total_price":"100.0"}]
how can i get this object in NSMutableArray ?
The data is not what you think it is. The value of the gas_cylinders key is an array of JSON string.
NSArray *cylinders = tripDictionary[#"gas_cylinders"];
NSString *firstCylinder = cylinders[0];
At this point, firstCylinder is a JSON string. You need to parse that JSON string to get the desired array of dictionaries contained in the JSON string.

Remove null objects from mutable array in objective c

I have a mutable array of checked box
NSMutableArray *arrayOfCheckedBox = [NSMutableArray arrayWithObjects:namePropertyString, lastNamePropertyString, companyPropertyString, workEmailPropertyString, personalEmailPropertyString, workPhonePropertyString, cellNumberPropertyString, nil];
[arrayOfCheckedBox removeObjectIdenticalTo:[NSNull null]]; //not working
NSLog(#"array of check box = %#", arrayOfCheckedBox);
If I click on check boxes at index 0, 1 and 4, it will only collect object at indexes 0 and 1 only and will not detect index 4 at all.
I get the values at the selected index in log before getting it in arrayOfCheckedBox. How to get checked values in this case?
The problem is that you're hitting a nil value, so the arrayWithObjects: method thinks you're at the end of the list of objects.
Something like this will work:
NSMutableArray *arrayOfCheckedBox = [NSMutableArray arrayWithCapacity:7];
if (namePropertyString)
[arrayOfCheckedBox addObject:namePropertyString];
if (lastNamePropertyString)
[arrayOfCheckedBox addObject:lastNamePropertyString];
I would suggest storing the check box values in an array of BOOLs.
Wrap them in NSNumbers first and unwrap them to retrieve them.
Wrap to store,
[NSNumber numberWithBool:YES]]
Unwrap to retrieve,
[[your_array objectAtIndex:index] boolValue]
For null values appear to be string literals #"" rather than the NSNull.So use following way to remove null object :
NSLog(#"%d", [arrayOfCheckedBox count]);
NSString *nullString = #"<null>";
[arrayOfCheckedBox removeObject:nullString];
NSLog(#"%d", [arrayOfCheckedBox count]);

Objective C NSPredicate predicateWithBlock removing nil/null values

I am trying to populate an array by taking an existing array and removing nil values from it. The array was populated from a the JSON response of an http call. Sometimes the array has a null value at the end, and the easiest way to remove that value so I wouldn't have to handle it everywhere in my code would be to use NSArray's filteredArrayUsingPredicate: to assign the variable into the instance variable I use throughout my class.
NSArray *respAgencyList = (NSArray*) [JSON valueForKeyPath:#"xml.path.to.data" ];
NSLog(#"data before filter: %#", respAgencyList);
// prints: ( { domain: "foo.com", name:"foobar"}, "<null>" });
if (respAgencyList != nil && respAgencyList.count > 0) {
agencies = [respAgencyList filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
NSLog(#"Evaluated object is %#", evaluatedObject); //prints <null> for the null value
BOOL ret = evaluatedObject != nil;
return ret;
}]];
}
In the above code the return value is always YES. However, when I put the debugger on and step through it I see:
evaluatedObject = id 0x00000000
Isn't this a null/nil value? What is different about this value compared to nil?
You should also check for NSNull, which can be placed into an NSArray since it is a proper object.
BOOL ret = (evaluatedObject != nil && [evaluatedObject isKindOfClass:[NSNull class]] == NO);
It is impossible for an NSArray to contain a nil element.
Some enumeration methods do hand you nil after the enumeration, as a signal that you've reached the end, but the nil is not in the array — it's just a signal, and you are not expected to do anything serious with it. However, I do not know whether this is one of them.
I suggest that instead of trying to remove nil from the array, which is impossible since nil was never there in the first place, you examine the array directly (log it, look in the debugger, whatever) and assure yourself that what you're trying to do is unnecessary.

extracting data from an array of arrays

I have the following code:
NSString *movies = [[NSString alloc] initWithData:webData7 encoding:NSUTF8StringEncoding];
NSLog(#"movies: %#", movies);
array_webdata = [parsedata objectWithString:movies error:nil];
userMovies = [array_webdata valueForKey:#"movies"];
NSLog(#"userMovies: %#", userMovies);
NSMutableArray *arrayCodes = [userMovies valueForKey:#"rtid"];
NSLog(#"arrayCodes: %#", arrayCodes);
In first line I save in movies the following:
movies:
{"movies":[["rtid","770672122"],["rtid","771268706"],["rtid","771240265"],["rtid","9377"]]}
with: userMovies = [array_webdata valueForKey:#"movies"]; I store in userMovies:
userMovies: (
rtid,
770672122
),
(
rtid,
771268706
),
(
rtid,
771240265
),
(
rtid,
9377
)
From here I want to extract the numbers to create another array like :
codes: (770672122, 771268706, 771240265, 9377)
But with following line, the execution crashes:
NSMutableArray *arrayCodes = [userMovies valueForKey:#"rtid"];
The error is EXC_BAD_ACCESS. On Debug Area shows no errors
Do you know what is wrong?
Thanks!
I have edited the question to make it more understandable
The code you have is clearly not going to work because you are assigning a number (or int, or some type) to a mutable array. You can't do that. You need to ADD them to the array since they will be contained IN THE ARRAY, those numbers ARE NOT arrays...
How are you getting it from JSON? Your array is probably an array of NSDictionary's so you'll want to do something like:
for (NSDictionary *aDictionary in userMovies)
{
[arrayCodes addObject: [aDictionary valueForKey:#"rtid"]];
}
You need to start by finding out what's in your array. Do:
NSLog(#"My array contains %#'s", [[userMovies valueForKey:#"rtid"] class]);
From there, if you get NSDictionary use the code above, and if you get an array, you'll have to loop through the array like:
for (NSArray *anArray in userMovies)
{
[arrayCodes addObject: [anArray objectAtIndex:1]];
}
SOLVED!
I have deleted my last two lines of code:
NSMutableArray *arrayCodes = [userMovies valueForKey:#"rtid"];
NSLog(#"arrayCodes: %#", arrayCodes);
and added the following
NSMutableArray *moviesCodes = [[NSMutableArray alloc] init];
for (int counter = 0; counter<[userMovies count]; counter++){
[moviesCodes addObject:[[userMovies objectAtIndex:counter] objectAtIndex:1]];
}
With this I have stored all the ids in the array called moviesCodes. The problem was that userMovies is an array of arrays

NSMutableArray with Integer

I have this mutable array:
myIntegers = [NSMutableArray array];
[myIntegers addObject:[NSNumber numberWithInteger:indexDelete - 1]];
NSLog (#"Array: %#", myIntegers);
If I execute the code twice, with indexDelete first being 1 and then 2, I get this result:
Array: (
1
)
and then:
Array: (
2
)
But I would like to store both numbers like this:
Array: (
1
2
)
Why is it not adding, but replacing the object??
You're creating a new empty array every time that code executes, on this line:
myIntegers = [NSMutableArray array];
Adding an object to an empty array always leads to there being one object in that array.