Transforming NSMutableArray values [closed] - iphone

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];

Related

how to convert NSMutable array with NSNumber into strings [closed]

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);
}

Creating a sorted array from a string and an array [closed]

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.
I'm trying to create a sorted array but im having trouble understanding how I would created it.
I have a string (s1) and another array (a1,a2,a3,....).
I want to create a new array using the string and array. I would like to put them in this order (s1 - ar - s1 - ar - s1 -ar).
*ar = the original array in a random order.
How would I go about that creating this array?
Thanks for any help
*Edit: I would like a shuffled array having s1 string at every alternate index
If I understand is correctly you need to create a new array and then one by one fill it up. So like insert the string then take a random element from the array till you run out of elements.
If you want to modify the original array then it has to be mutable (what language are you using?)
in objective-c it would be something like this:
NSString* string;
NSArray* array;
NSMutableArray* temparray = [NSMutableArray arrayWithArray:array];
NSMutableArray* result = [[NSMutableArray alloc] initWithCapacity:[array count]*2];
for (int i=0; i< [array count];i++) {
[result addObject:string];
int index = rand() % [temparray count];
[result addObject:[temparray objectAtIndex:index]];
[temparray removeObjectAtIndex:index];
}
Have a look at NSMutableArray:
NSMutableArray *array = [NSMutableArray arrayWithObjects:#"a1", #"a2", #"a3",nil];
[array insertObject:#"s1" atIndex:0]; // add as first object
[array addObject:#"s2"]; // add as last object
// sort
[array sortUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
// swap elements
[array exchangeObjectAtIndex:1 withObjectAtIndex:2];
for (NSString *s in array)
{
NSLog(#"elemnt: %#", s);
}
I ended up shuffling the array and adding the string to every even index.
NSMutableArray *array = [NSMutableArray arrayWithObjects:#"a1", #"a2", #"a3", #"a4", #"a5",nil];
[array shuffle];
for (int i = 0 ; i<array.count; i=i+2) {
[array insertObject:#"s1" atIndex:i];
}
NSLog(#"%#",array);
This is how I shuffled the array
NSMutableArray+Shuffling.h
#interface NSMutableArray (Shuffling)
- (void)shuffle;
#end
NSMutableArray+Shuffling.m
#import "NSMutableArray+Shuffling.h"
#implementation NSMutableArray (Shuffling)
- (void)shuffle
{
for (uint i = 0; i < self.count; ++i)
{
// Select a random element between i and end of array to swap with.
int nElements = self.count - i;
int n = arc4random_uniform(nElements) + i;
[self exchangeObjectAtIndex:i withObjectAtIndex:n];
}
}
#end
This I what the output looked like
Array (s1,
a3,
s1,
a5,
s1,
a4,
s1,
a2,
s1,
a1)
Sorry If the question was poorly worded.

How can i check the array has object or not [closed]

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.
i want to check if the old array has object or not if the old array has the object it should show me the button if the oldArray has zero object the button should be hidden the code is given below thanks...
-(void)viewWillAppear:(BOOL)animated
{
GET_DEFAULTS
NSMutableArray *array = [defaults objectForKey:kShouldResume];
NSData *dataRepresentingSavedArray = [defaults objectForKey:kShouldResume];
if (dataRepresentingSavedArray != nil)
{
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
if (oldSavedArray != nil)
{
array = [[NSMutableArray alloc] initWithArray:oldSavedArray];
if ([oldSavedArray containsObject])
{
btnResumeGame.hidden=NO;
}
else
{
btnResumeGame.hidden=YES;
}
}
else
{
array = [[NSMutableArray alloc] init];
}
}
}
Array has property count.
You can check weather count is zero or more than that as you require..
like
oldSavedArray.count
use this code:
if ( [oldSavedArray count]>0 ){
btnResumeGame.hidden=NO;
}
else{
btnResumeGame.hidden=YES;
}

Get index of array object [closed]

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
}
}
}

Filter an nsmutable array issue [closed]

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.