It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
#interface demodata : NSObject
{
NSString *Day;
NSString *content;
#property (nonatomic, retain) NSString *Day;
#property (nonatomic, retain) NSString *content;
}
-------
Test.m file--
NSMutableArray *sessions = [[NSMutableArray alloc] init];
demodata * sess = [[demodata alloc] init];
sess.Day=#"Monday";
sess.content=#"HI";
[sessions addObject :sess];
[sess release];
demodata * sess1 = [[demodata alloc] init];
sess1.Day=#"Tuesday";
sess1.content=#"Bye";
[sessions addObject :sess1];
[sess1 release];
I tried
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"Day == %#", #"Monday"];
NSArray *filteredArray = [sessions filteredArrayUsingPredicate:predicate];
my array object is class(nsobject)..
It's not working...
How to i filter the array(sessions) by daywise..
You could use NSArrays -filteredArrayUsingPredicate: and pass an NSPredicate describing your needs.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"Day = %#", #"Tuesday"];
NSArray *filteredArray = [myArray filteredArrayUsingPredicate:predicate];
This code working perfectly..The problem is there is some whitespace in the array object.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have parsed data stored in an NSObject file. I then stored the object file in an array:
[array addobject: object];
When i print the array, it only contains the first object value.
I declared the array globally, alloced memory for it and allocated memory for the object too.
It is printed like this:
array count 2 and array is:
(
"about 1st orders",
"about 2nd orders"
)
class is __NSArrayM
Previously it is correctly storing entire objects.
Any ideas or suggestions to solve this?
Following is describe Logic for You , might be helpful in you case.
First Create one class (name As you want)
#import <Foundation/Foundation.h>
#interface MyClassName : NSObject {
NSString *name; // set as per your requirement.
NSString *address; // set as per your requirement.
.
.
.
}
#property (retain, readwrite) NSString * name;
#property (retain, readwrite) NSString * address;
.
.
.
and Properly #synthesize and release reference. (if Not use ARC)
And add your array in this Class Object And then Get Data such like,
EDIT:
NSMutableArray *anOtherArray = [[NSMutableArray alloc] init];
for(int i=0; i < listOfArray.count; i++)
{
MyClassName *myData = [self.listOfArray objectAtIndex:i];
NSLog(#"%#", myData.name);
NSLog(#"%#", myData.address);
[anOtherArray addObject:myData.name];
[anOtherArray addObject:myData.address];
}
What is the definition of your object that you're storing in the NSArray? For custom objects being stored in an array, you should override NSObject's isEqual method.
For custom objects being stored in maps such as NSDictionary, you'll want to implement hashcode too.
Here is what i usually do when putting objects into array :
#import <Foundation/Foundation.h>
#interface ImageObject : NSObject
{
NSString *ImageURL;
NSString *Date;
// put any other stuff here.
}
#property (nonatomic, strong) NSString *ImageURL;
#property (nonatomic, strong) NSString *Date;
// put any other stuff here too.
#end
Then when you want to insert the object :
NSMutableArray *yourMutableArray = [[NSMutableArray alloc]init];
NSArray *array = [[NSArray alloc]initWithArray:[self.imageDictionary objectForKey:#"images"]];
for (NSDictionary *dict in array) {
ImageObject *imgObj = [[ImageObject alloc]init];
imgObj.ImageURL = [dict objectForKey:#"ImageURL"];
imgObj.Date = [dict objectForKey:#"LogDate"];
[yourMutableArray addObject:imgObj];
}
and for calling the items inside the array :
ImageObject *imgObj = [[ImageObject alloc]init];
imgObj = [yourMutableArray objectAtIndex:indexOfArray];
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
my array is containing ids they are in NSNumber how can i convert them in string my array is like below
1494447926,
1537064431,
1545735176,
1574825141,
1604834983,
1829486110,
1838260338,
1846543841,
1850381039,
100000039842949,
100000077723868,
100000103091995,
100000126558358,
100000130915431,
100000139092102,
100000157330187,
100000157646688,
100000197141710,
100000243178639,
100000249947961,
please give me sample code to convert it to string
First of all array can not store integer. It must be in NSNumber or it is in NSString itself.
In either of the case you can create a long string by appending them,
NSString *string=[yourArray componentsJoinedByString:#","];
Or, if you want each value as string then you need to create that much string and then access them.
NSArray *numbersToStrings=[NSArray new];
for(id element in yourArray){
[numbersToStrings addObject:[NSString stringWithFormat:#"%#",element];
}
Here numbersToStrings contains all the values as string.
Use this.
NSString *str = [NSString stringWithFormat:#"%i",number];
for(int i=0;i<[arr count];i++){
str = [NSString stringWithFormat:#"%d",[arr objectAtIndex:i]];
[newArr addObject:str];
}
NSString *str = [NSString StringWithFormat:#"%d",1494447926];
You can use stringWithFormat
NSString *str = [NSString stringWithFormat:#"%d", [YourArray objectAtIndex:index]];
You cannot store integers into an array. If you are getting this response from server each would be NSNumber. You can type cast that to NSString.
do this
NSArray *ll=[NSArray arrayWithObjects:#"1",#"2",#"3", nil];
NSString *strinList=[NSString stringWithFormat:#"%#",[ll objectAtIndex:0]];
try this ,if you required other help ,i am here .
Only Search on Google - convert int to NSString , multiple Answer are displayed
by the way, your need to use only [NSString stringWithFormat:#"%d",YourIntValue]
for (int i=0; i < MyArray.count; i++)
{
NSString * String =[NSString stringWithFormat:#"%d", [MyArray objectAtIndex:i]];
NSLog(#"%#",String);
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Array: (2007-99 , 2001-96, 2005-93)
Sorted Output should be: (2005-93, 2001-96, 2007-99)
Please help me out.
You need to write a custom comparator to do something like this. In the method below, I get the location of the dash with rangeOfString, then get the substring starting 1 position further into the string, then convert that to an int to do the comparison:
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:#"2007-07",#"2005-01",#"2004-09",#"2003-02", nil];
NSArray *sortedArray = [arr sortedArrayUsingComparator: ^(NSString *s1, NSString *s2) {
if ([[s1 substringFromIndex:[s1 rangeOfString:#"-"].location + 1] intValue] > [[s2 substringFromIndex:[s2 rangeOfString:#"-"].location + 1] intValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([[s1 substringFromIndex:[s1 rangeOfString:#"-"].location + 1] intValue] < [[s2 substringFromIndex:[s2 rangeOfString:#"-"].location + 1] intValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSLog(#"%#",sortedArray);
You can sort this using a custom block (note that I assume that all of your numbers are formatted correctly):
NSArray *rollNumbers = [NSArray arrayWithObjects:#"2007-99", #"2001-96", #"2005-93", nil];
NSArray *sortedRollNumbers = [rollNumbers sortedArrayUsingComparator:^NSComparisonResult(NSString *roll1, NSString *roll2) {
NSArray *roll1Components = [roll1 componentsSeparatedByString:#"-"];
NSArray *roll2Components = [roll2 componentsSeparatedByString:#"-"];
NSNumber *roll1Number = [NSNumber numberWithInt:[[roll1Components objectAtIndex:1] intValue]];
NSNumber *roll2Number = [NSNumber numberWithInt:[[roll2Components objectAtIndex:1] intValue]];
return [roll1Number compare:roll2Number];
}];
NSLog(#"%#", sortedRollNumbers);
Output:
(
"2005-93",
"2001-96",
"2007-99" )
You can sort your array like this :
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:#"2007-07",#"2005-01",#"2004-09",#"2003-02", nil];
NSMutableArray *marks = [[NSMutableArray alloc]init];
for (int i = 0; i < arr.count; i++)
{
NSArray *sep = [[arr objectAtIndex:i] componentsSeparatedByString:#"-"];
[marks addObject:[sep objectAtIndex:1]];
}
NSSortDescriptor *sortDesc = [[NSSortDescriptor alloc] initWithKey:#"" ascending:NO];
[marks sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
NSMutableArray *sortedFinalArray = [[NSMutableArray alloc]init];
for (int i = 0; i < marks.count; i++)
{
for (int k = 0; k < arr.count; k++)
{
NSRange aRange = [[arr objectAtIndex:i] rangeOfString:[marks objectAtIndex:k]];
if (!(aRange.location == NSNotFound))
{
[sortedFinalArray addObject:[arr objectAtIndex:k]];
}
}
}
In order that you can sort your array, the elements of the array have to be compared pairwise to find out their ordering. Your specific ordering is custom, so you have to write a compare method (e.g. named compare:) by yourself, and then you can use [arr sortUsingSelector:#selector(compare:)]; to sort your array.
Now the compare: method has to be known to the elements of the array, because each element uses it to compare it to another element of the same class. So either you define a new class for your elements that implements the compare method, or you leave them as NSStrings, buth the you have to define a category that implements the compare: method.
The compare: method itself could look like this (pseudo code):
-(NSComparisonResult) compare: (MyString *) myString {
if
(self.the_last_2_characters_interpreted_asNumber <
myString.the_last_2_characters_interpreted_asNumber)
return NSOrderedAscending;
else if
(self.the_last_2_characters_interpreted_asNumber ==
myString.the_last_2_characters_interpreted_asNumber)
return NSOrderedSame;
else
return NSOrderedDescending;
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
This is the code I'm trying to finish
-(IBAction)theButtonIsSelected:(id)sender {
NSMutableDictionary *mutDict = [NSMutableDictionary dictionaryWithDictionary:[detailsDataSource objectAtIndex:detailIndex]];
[mutDict setObject:#"Yes" forKey:#"Favorite"];
NSString *nameString = [mutDict valueForKey:#"Name"];
NSArray *allObjects;
allObjects = [[NSArray alloc] initWithContentsOfFile:path];
NSMutableArray *tmpMutArr = [NSMutableArray arrayWithArray:allObjects];
int index;
//I think I just need a little piece right here to set the current allObjectsIndex to match nameString?
[tmpMutArr replaceObjectAtIndex:index withObject:[NSDictionary dictionaryWithDictionary:mutDict]];
allObjects = nil;
allObjects = [[NSArray alloc] initWithArray:tmpMutArr];
[allObjects writeToFile:path atomically:YES];
}
This is my question:
if (what I'm trying to do above can be done) {
How to finish it?
} else {
How to make a function to change the "Favorite" key's value of plist object,
when detailsDataSource not always containing the complete list of objects?
That's why I'm trying to include allObjects and index in this code.
}
EDIT:
Code now look like this:
NSMutableDictionary *mutDict = [NSMutableDictionary dictionaryWithDictionary:[detailsDataSource objectAtIndex:detailIndex]];
[mutDict setObject:#"Yes" forKey:#"Favorite"];
NSString *nameString = [[detailsDataSource objectAtIndex:detailIndex] valueForKey:#"Name"];
NSArray *allObjectsArray = [[NSArray alloc] initWithContentsOfFile:path];
NSMutableArray *tmpMutArr = [NSMutableArray arrayWithArray:allObjectsArray];
if(int i=0;i<[tmpMutArr count];i++)
//Errors ^here and here^
{
if([[tmpMutArr objectAtIndex:i] isKindOfClass:[NSDictionary class]])
{
NSMutableDictionary *tempDict = [tmpMutArr objectAtIndex:i];
if([tempDict valueForKey:#"Name" == [NSString stringWithFormat:#"#%", nameString];) //Is this correct?
{
index = i; //index of dictionary
}
}
}
[tmpMutArr replaceObjectAtIndex:i withObject:[NSDictionary dictionaryWithDictionary:mutDict]];
allObjectsArray = nil;
allObjectsArray = [[NSArray alloc] initWithArray:tmpMutArr];
[allObjectsArray writeToFile:path atomically:YES];
Errors: 1 Expected expression 2 undeclared identifier 'i' how to declare I and fix the other error?
You can get index of dictionary like this:
if(int i=0;i<[tmpMutArr count];i++)
{
if([[tmpMutArr objectAtIndex:i] isKindOfClass:[NSDictionary class]])
{
NSMutableDictionary *tempDict = [tmpMutArr objectAtIndex:i];
if([tempDict objectForKey:#"Favorite")
{
index = i; // here u have your index of dictionary
}
}
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I've been looking for this, but can't find the answer.
I have an NSMutableArray with values who_1, what_2, where_3 etc.
I want to transform this into who, what, where etc.
I already have the value of the integer as a variable, and _ is just a string.
What steps should I take to have all these arrayvalues transformed?
NSArray * arrB = [[NSArray alloc]initWithObjects:#"apple_a",#"ball_b",#"cat_c",#"doll_d",nil];
NSMutableArray * arrA = [[NSMutableArray alloc]init];
for(NSString *strData in arrB)
{
NSArray *arr = [strData componentsSeparatedByString:#"_"];
[arrA addObject:[arr objectAtIndex:0]];
}
and this would be your output
arrA:(
apple,
ball,
cat,
doll
)
You need to apply logic for that, You cant find answers to tricky Questions :)
You need to run a loop.
Separate string with '_'
Loop
for(NSString *s in ary)
{
NSArray *a = [s componentsSeparatedByString:#"_"];
[anotherArray addObject:[a objectAtIndex:0]];
}
and update your array..
Following might help you -
NSRange range = [string rangeOfString:#"_"];
NSString *finalString = [originalString substringToIndex:range.location];
you can have this in loop.
Or you can go for componentSeperatedByStrings.
This might help you
NSMutableArray *tmpAry = [[NSMutableArray alloc] init];
for(NSString *_string in _StringAry)
{
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:#"_0123456789"];
_string = [[_string componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:#""];
[tmpAry addObject: [[_string copy] autorelease]];
}
NSLog(#"%#", tmpAry); // Gives the modified array
[tmpAry release];