I get the response from service url and I took them into array.I have the objects in this array with todays week day.But while doing so I get commas from service.So I need to remove the commas from them and compare.How can I do it?
array3:
(
{
Code = "Sunday,";
},
{
Code = "Thursday,";
},
{
Code = "Friday,";
},
{
Code = "Saturday,";
}
)
To compare:
NSMutableArray *yourArray = [[NSMutableArray alloc] init];
for (NSDictionary *dict in array3) {
[yourArray addObject:[dict valueForKey:#"Code"]];
}
BOOL isTheObjectThere = [yourArray containsObject:#"Thursday"];
But I get isTheObjectThere as 'NO' because yourArray is like:
(
Sunday,,
Thursday,,
Friday,,
Saturday,
)
So how can I remove that extra comma when there are two commas and compare:
Try this,
for (NSDictionary *dict in array3) {
NSString *str=[dict valueForKey:#"Code"];
NSString*newstr= [str stringByReplacingOccurrencesOfString:#"," withString:#""];
[yourArray addObject:newstr];
}
Use stringByReplacingOccurrencesOfString like this:
NSMutableArray *newArrayDays = [NSMutableArray array];
for(int i=0; i< [mutArrDays count]; i++) //your array of days here to remove commas from it
{
[newArrayDays addObject:[[mutArrDays objectAtIndex:i]stringByReplacingOccurrencesOfString#",," withString:#","]:
}
EDIT : you can also use replaceObjectAtIndex:withObject:
Related
I have a NSArray which contain n number of NSDictionary sampleArray =
(
{
0 = 0;
1 = 0;
},
{
0 = 86400;
1 = 2;
},
{
0 = 172800;
1 = 4;
},
{
0 = 259200;
1 = 5;
}
)
Now I need to fetch the NSArray for objectForKey 0 [sampleArray objectForKey:[NSNumber numberWithInt:0]], my result NSArray should be like
(0,86400,172800,259200) but I am unable to fetch the result and the app crashes.
Normally for NSDictionary, if key value is set using NSString valueForKey the above operation is performed successfully but if key value is set using an object like NSNumber objectForKey I am unable to perform the operation.
Please help me to get a solution, any suggestion would be appreciated!!
A very straight forward way if your keys are NSNumber objects:
NSMutableArray *result = [[NSMutableArray alloc] init];
for (NSDictionary *d in a) {
if (d[#0]) {
[result addObject:d[#0]];
}
}
THIS doesnt work - im sorry: I didnt see you didnt have Strings as keys + I didnt know KVC only works with strings
I leave it though
what you are looking for is
NSArray *zeros = [mainArray valueForKey:#"0"];
it gets "0" from each dict in array
What you have do is
NSMutableArray *arrResult = [[NSMutableArray alloc]init];
for(int i=0;i<[sampleArray count];i++)
{
NSString *strValue = [[sampleArray objectAtIndex:i] valueforkey:#"0"];
[ arrResult addobject:strValue];
}
let me know it is working or not!!!!
Happy coding!!!!!
here you can try NSArray *tempArray
for (NSDictionary *list in tempArray)
{
[firstArray addObject:[list objectForKey:#"0"];
[secondArray addObject:[list objectForKey:#"1"];
}
I have a NSMutableArray and I need to compare the content in it with a NSString..But couldn't get it ?
My array
(
{
Code=Sunday;
},
{
Code=Monday;
},
{
Code=Tuesday;
}
)
I m comparing like this :
BOOL isTheObjectThere = [array containsObject:weekday];
if([arr count]==0 && isTheObjectThere==YES)
{
//do something
}
else
{
//do something
}
Here weekday is NSString whose value=Sunday
But isTheObjectThere is returning NO..
Where Im going wrong?
see this example
NSMutableArray* array = [NSArray arrayWithObjects:#"Sunday", #"Monday", #"Tuesday", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF IN %#", array];
BOOL isTheObjectThere = [predicate evaluateWithObject:#"Sunday"];
NSLog(#"isTheObjectThere %d",isTheObjectThere);
its work fine and return isTheObjectThere 1
The array contains dictionary elements, then retrieve values of key(Code).
NSMutableArray *yourArray = [[NSMutableArray alloc] init];
for (NSDictionary *dict in array) {
[yourArray addObject:[dict valueForKey:#"Code"]];
}
BOOL isTheObjectThere = [yourArray containsObject:#"Sunday"];
BOOL isTheObjectThere = [array containsObject:weekday];
will not work, better try the following method.
NSMutableArray* inputArray = [NSArray arrayWithObjects:#"Sunday", #"Monday", #"Tuesday", nil];
for (NSString* item in inputArray)
{
if ([item rangeOfString:#"Sunday"].location != NSNotFound)
{
//do something
}
else
{
//do something;
}
}
NSString *yourString = #"Monday";
for (NSString* item in inputArray){
if ([yourString rangeOfString:item].location != NSNotFound)
//Do something;
else
// Do the other Thing;
}
I have an array with 4 Dictionaries with key #"preference" like as follows
(
{
preference = Nose;
},
{
preference = "Heart rate";
},
{
preference = Glucose;
},
{
preference = Food;
}
)
Now i want to retrieve an array for these dictionary values like"
(
Nose, Heart rate, Glucose, Food
)
How s'd i get it.. Thanks in advance
A one-liner:
NSArray *resultingArray = [arrayOfDictionaries valueForKeyPath:#"preference"];
Try it:
NSArray *result = [dictionaryObject valueForKeyPath:#"preference"];
It'll solve your Problem
Do something like this:
NSMutableArray *collectedValues = [NSMutableArray arrayWithCapacity:array.count];
for (NSDictionary *dict in array) {
NSString *value = [dict objectForKey:#"preference"];
if (value) {
[collectedValues addObject:value];
}
}
Try with this code:
myArray is your first array
mySecondArray is the array you have in the end
NSMutableArray *mySecondArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [myArray count] ; i++)
{
NSDictionary *tempDict = [myArray objectForIndex:i];
[mySecondArray addObject:[tempDict objectForKey#"preference"]];
}
i have this code :
NSMutableArray *arr = [NSMutableArray array];
for (int i = 0; i <[titleNews count]; ++i) {
TFHppleElement *someText =[titleNews objectAtIndex:i];
NSString *result = [someText content];
[arr addObject:result];
if ([[arr objectAtIndex:i]isEqualToString:#"-- : --"]) {
[arr replaceObjectAtIndex:i withObject:#"x"];
[arr addObject:#"/"];
}
NSLog(#"%#",[arr objectAtIndex:i]);
}
the (replaceObjectAtIndex: withObject:) work fine but the (addObject)method not working
You're presuming that the "result" that you add to the array will always be at index i. But the first time that you fall into your "if" body, you'll add an i+1 element ("/") and from then on everything will be off by one.
I have a table view, which has its data source from an array that contains names of people.
Now to make it easy to find people, I want to section the table view so that it has the letter A-Z on the right hand side, just like the Address Book app.
But my current array just contains a collection of NSStrings. How do I split them so that they are grouped by the first letter of the names? Is there any convenient way to do it?
EDIT: If anyone's interested in my final code:
NSMutableArray *arrayChars = [[NSMutableArray alloc] init];
for (char i = 'A'; i <= 'Z' ; i++) {
NSMutableDictionary *characterDict = [[NSMutableDictionary alloc]init];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (int k = 0; k < [myList count]; k++) {
NSString *currentName = [[friends objectAtIndex:k] objectForKey:#"name"];
char heading = [currentName characterAtIndex:0];
heading = toupper(heading);
if (heading == i) {
[tempArray addObject:[friends objectAtIndex:k]];
}
}
[characterDict setObject:tempArray forKey:#"rowValues"];
[characterDict setObject:[NSString stringWithFormat:#"%c",i] forKey:#"headerTitle"];
[arrayChars addObject:characterDict];
[characterDict release];
[tempArray release];
}
At the end of the function I'll have:
arrayChars [0] = dictionary(headerTitle = 'A', rowValues = {"adam", "alice", etc})
arrayChars[1] = dictionary(headerTitle = 'B', rowValues = {"Bob", etc})
Thank you everyone for your help!
You can use a dictionary to sort them, so create an array with all the letters you want to sort and a array with nil objects to initialize the dictionary
NSArray *names = #[#"javier",#"juan", #"pedro", #"juan", #"diego"];
NSArray *letters = #[#"j", #"p", #"d"];
NSMutableArray *objects = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < [letters count]; ++i)
{
[objects addObject:[[NSMutableArray alloc] init]];
}
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:objects forKeys:letters];
Then you must find the first letter if the word and put that word into the corresponding key in the dictionary
for (NSString *name in names) {
NSString *firstLetter = [name substringToIndex:1];
for (NSString *letter in letters) {
if ([firstLetter isEqualToString:letter]) {
NSMutableArray *currentObjects = [dictionary objectForKey:letter];
[currentObjects addObject:name];
}
}
}
To check you can print directly the dictionary
NSLog(#"%#", dictionary);
Then is your work to fill your sections in the tableview using the dictionary