Want to store NSRange and NSInteger inside NSMutableDictionry [duplicate] - iphone

This question already has answers here:
Passing NSInteger variable to NSMutableDictionary or NSMutableArray
(7 answers)
Closed 9 years ago.
I need to store both the NSRange object and NSInteger inside the NSMutableDictionary?
Can I do it?
If it is, Could you give me an example?

You can store the NSInteger as NSNumber object.
mutableDictionary[integerKey] = [NSNumber numberWithInteger:integer];
or even better
mutableDictionary[integerKey] = #(integer);
For NSRange, use NSValue object.
mutableDictionary[rangeKey] = [NSValue valueWithRange:range];

I have used NSString for both NSRange and NSInteger, to remove confusion use this:
NSInteger i = 6;
NSRange range = NSMakeRange (25, 3);
NSMutableDictionary *dic =[[NSMutableDictionary alloc] init];
[dic setObject:NSStringFromRange(range) forKey:#"range"];
[dic setObject:[NSString stringWithFormat:#"%d",i] forKey:#"integer"];
//How to get it...
NSRange range1 = NSRangeFromString([dic objectForKey:#"range"]) ;
NSInteger inte = [[dic objectForKey:#"integer"] integerValue];

Related

How to concatenate all values of array in Xcode [duplicate]

This question already has answers here:
Join an Array in Objective-C
(3 answers)
Closed 9 years ago.
I am new in iOS and i am not able to find solution of my question . So please tell me how can i concatenate all values of array in iOS
Answer : -componentsJoinedByString: .
It will join the components in the array by the specified string and return a string representation of the array.
Use : NSString *finalStr = [yourArray componentsJoinedByString:#" "];
GoodLuck !!!
if you are using string values in NSArray then it is quite simple.
You can just run for loop and use a NSMutableString to append the data.
NSArray *_array;//Your Initialized Array
NSMutableString *_string =[[NSMutableString alloc]init];
for(int i= 0 ;i<[_array count];i++)
{
[_string appendFormat:#"%#",[_array objectAtIndex:i]];
}
NSLog(#"_string = %#",_string);
NSMutableString* theString = [NSMutableString string];
for (int i=0; i<[array count];i++){
[theString appendString:[NSString stringWithFormat:#"%i ",i]];
}
NSLog(#"mystirng=%#", theString;);

How to get last array values from NsmutableArray [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get the last object of an NSArray
In my iPhone application, I want to retrieve the last array value from NSMutableArray:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:messages];
NSLog(#"array : %#", array);
In this "array". I having this below values.
hi, hello,
I want to take hello values only from the NSMutableArray.
How can I do this?
Try this
NSLog(#"array : %#",[array lastObject]);
It may help's you
you should follow indexpath at [array count] - 1 to obtain the last value.
For Ex,
NSString *str = [NSString stringWithFormat:"%#",[array objectAtIndex:[array count] - 1]];
NSLog("last object of an array = %#",str);
Enjoy Programming!
You can use:
NSString *str = (NSString *)[array lastObject];
[array lastObject] returns the last object of the array.
NSArray *arr=[NSArray arrayWithObjects:#"A",#"B",#"C", nil];
NSString *last=[arr lastObject];
NSLog(#"Last obj in string : %#",last);
Output :
Last obj in string : C

NSMutableString append is not working [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Objective-C: How do you append a string to an NSMutableString?
I have a NSMutableArray. I want to extract all elements of this NSMutable array and append it in to a string. My following code is not working. String is not append to the variable. Also I am using automatic reference counting feature in this project.
NSMutableString *theString = [NSMutableString string];
for(int i=0; i<[self.incomingMessages count]; i++) {
id object = [self.incomingMessages objectAtIndex: i];
[theString appendString:(NSString*)object];
}
NSLog(#"the appended string: %#", theString);
[self.incomingMessages removeAllObjects];
How can I append the stored strings into the theString variable. Whats is the mistake I have done?
Do this
NSMutableString *theString = [[NSMutableString alloc]init ];
Try to print and see if your object is string
Maybe you need to initialize your theString like this?
NSMutableString *theString = [NSMutableString stringWithString:#""];

how to converts NSMutableArray values into NSString? [duplicate]

This question already has answers here:
How can I convert NSMutableArray into NSString?
(3 answers)
Closed 9 years ago.
hi all how to convert NSMutableArray values into the NSString
when i did as like
Webservices *details=[[Webservices alloc] init];
[details getMobileNumberDetails:phnotextfield.text];
NSLog(#"longitudes Arrays from %#",details.resultData);
"-91.57696007",
"10.343234",
"74.982343",
"76.464844",
"76.464844",
"2.256",
but when implemented like this
theCoordinate2.longitude = [[details.longarray objectAtIndex:0] floatValue];
NSLog(#"longitudes list from %#",theCoordinate2.longitude);
-91.57696007
but my intention is to assign all values to theCoordinate2.longitude so help me to store into string element in iphone.
You can use this code:
theCoordinate2.longitude = [[details.longarray objectAtIndex:0] stringByAppendingString:[details.longarray objectAtIndex:1];
//and so on for index 2,3,4 stringByAppendingString:[details.longarray objectAtIndex:2
NSLog(#"longitudes list from %f",theCoordinate2.longitude);
+1 to Wasif, but while he was composing his answer I was composing my own version of the code you could try:
NSMutableArray * longitudeArray = NULL;
NSString * detailsAsString = details.resultData;
if(detailsAsString)
{
NSArray * longitudeStringArray = [detailsAsString componentsSeparatedByString, #","];
if(longitudeStringArray && ([longitudeStringArray count] > 0))
{
longitudeArray = [[NSMutableArray alloc] initWithCapacity: [longitudeStringArray count]];
if(longitudeArray)
{
for(NSString * longitudeString in longitudeStringArray)
{
[longitudeArray addObject: [NSNumber numberWithFloat: [longitudeString floatValue]]];
}
}
} else {
NSLog( #"did not extract an array out of webservice data");
}
}

Syntax help - Variable as object name [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
create multiple variables based on an int count
Objective C Equivalent of PHP's “Variable Variables”
How would I create and reference an object using a variable as it's name?
Example -
for (int i=1; i<7; i++) {
CGRect ("myRectNum & i") = myImageView.bounds;
}
("myRectNum & 5").height etc ..
There isn't anything like this in the Objective-C language, and in general it's not going to be a very practical way of referring to data (what if you typo a string? the compiler won't be able to catch it). I won't get into second-guessing what you actually want to do (that would depend on the goal of this part of your application), but you can use an NSMutableDictionary to get a similar effect:
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for (int i = 0; i < 7; i++)
{
NSString *key = [NSString stringWithFormat:#"myRectNum & %d", i];
NSValue *value = [NSValue valueWithCGRect:myImageView.bounds];
[dict setObject:value forKey:key];
}
Then to fetch the values back out again:
NSValue *value = [dict objectForKey:#"myRectNum & 5"];
CGRect bounds = [value CGRectValue];
NSLog(#"height = %f", bounds.size.height);