I've got strange output from 'componentsSeparatedByString' method of NSString - ios

I want to store the array of NSDictionary to a file. So I write a function to convert from NSArray to NSString. But I got a very strange problem. Here is my code.
+ (NSArray *)arrayForString:(NSString*)dataString
{
NSArray* stringArray = [dataString componentsSeparatedByString:ROW_SEPARATOR];
NSLog(#"%#", stringArray);
NSMutableArray* dictionaryArray = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < [stringArray count]; i++)
{
NSString* string = [stringArray objectAtIndex:i];
NSLog(#"%#", string);
NSArray* subStrings = [string componentsSeparatedByString:COLUMN_SEPARATOR];
NSDictionary* dic = [[NSDictionary alloc] initWithObjectsAndKeys:[subStrings objectAtIndex:0], PHOTO_NAME, [NSNumber numberWithUnsignedInt:[[subStrings objectAtIndex:1] unsignedIntValue]], PHOTO_SEQ_NO, nil];
[dictionaryArray addObject:dic];
}
return dictionaryArray;
}
Here is the log:
2012-05-05 23:57:35.113 SoundRecognizer[147:707] (
"new Photo/0",
"new Photo/1"
)
2012-05-05 23:57:35.118 SoundRecognizer[147:707] new Photo/0
2012-05-05 23:57:35.123 SoundRecognizer[147:707] -[__NSCFString unsignedIntValue]: unrecognized selector sent to instance 0x1d18c0
How do I get a #"-" from this following array?!
2012-05-05 23:57:35.113 SoundRecognizer[147:707] (
"new Photo/0",
"new Photo/1"
)

NSString doesn't have an unsignedIntValue method. Use intValue instead. But I'm not sure of the point of all this - you can write an array of dictionaries straight to a file anyway (as long as they only contain property list types) using writeToFile: atomically:.

Related

How to print the contents of NSSet?

I query and get a NSSet which contains customer address from web. Since I'm new to objective c development I don't know how to get country,zip code etc from that set.So I followed Objective-C How to print NSSet on one line (no trailing comma / space) but my output is in the form of object "0x7f99997b7a50". How can I print all the strings in the set? Thanks in advance.
I tried like this
NSArray *ar = [customer.addresses allObjects];
for (int i = 0; i<ar.count; i++)
{
NSLog(#"arr %#",ar[i]);
}
But the output is arr:
<BUYAddress: 0x7fd451f6e050>
If you have a custom object, you may need to override description
Without overriding:
-(void) testCustomObjects
{
CustomObject *co1 = [[CustomObject alloc] init];
co1.name = #"James Webster";
co1.jobTitle = #"Code Monkey";
CustomObject *co2 = [[CustomObject alloc] init];
co2.name = #"Holly T Canine";
co2.jobTitle = #"Pet Dog";
NSSet *set = [NSSet setWithObjects:co1, co2, nil];
NSLog(#"%#", [set allObjects]);
}
produces:
2016-12-02 11:45:55.342 Playground[95359:4188387] (
"<CustomObject: 0x600000037a20>",
"<CustomObject: 0x60000003ae20>"
)
However, if I override the description method in my CustomObject class:
-(NSString*) description
{
return [NSString stringWithFormat:#"%# (%#)", self.name, self.jobTitle];
}
I get the following:
(
"Holly T Canine (Pet Dog)",
"James Webster (Code Monkey)"
)
If for whatever reason, you can't add a description method, you'd just have to access the relevant parts of the object; something like the following:
NSArray *ar = [customer.addresses allObjects];
for (int i = 0; i<ar.count; i++)
{
NSLog(#"arr %# (%#)",ar[i].name, ar[i].address);
}
I've had a little look at the library you're using. Try the following:
for (BUYAddress *address in customer.addresses)
{
NSLog(#"Address: %#, %#, %#", address.address1, address.address2, address.city);
}
Consider NSSet below,
NSSet *theNSSet = [NSSet setWithObjects:#"Chennai",#"Mumbai",#"Delhi", nil];
Convert it into NSArray using
NSArray *array = [theNSSet allObjects]; // theNSSet is replaced with your NSSet id
Then print it like
NSLog(#"%#",array);
Output im getting
(
Chennai,
Delhi,
Mumbai
)
In your case:
- (NSMutableSet *)addressesSet {
[self willAccessValueForKey:#"addresses"];
NSMutableSet *result = (NSMutableSet *)[self mutableSetValueForKey:#"addresses"];
[self didAccessValueForKey:#"addresses"];
NSLog(#"%#",[result allObjects]); // printing the nsset
return result;
}

Separate a String By Semicolon

I am using following code to separate a string into multiple strings and getting an error
NSArray *arr = [randomStr componentsSeparatedByString:#";"];
Error:
-[__NSDictionaryM componentsSeparatedByString:]: unrecognized selector sent to instance 0x1758f230
-[__NSDictionaryM componentsSeparatedByString:]: unrecognized selector sent to instance 0x1758f230
This is my Sample Data
NSArray *data = {
{
name = "name1";
address = "RWP";
ID = 0;
},
{
name = "name2";
address = "RWP";
ID = 1;
},
{
name = "name3";
address = "RWP";
ID = 2;
},}
NSString *randomStr = data[0];
What's wrong in my code
You have an array of dictionaries, not strings. There is nothing to split.
You want something like this:
NSDictionary *dict = data[0];
NSString *name = dict[#"name"];
NSString *address = dict[#"address"];
You have to be sure randomStr as a NSString.
You can check like
if ([randomStr isKindOfClass:[NSString Class]]) {
NSArray *arr = [randomStr componentsSeparatedByString:#";"];
}
It sounds like the randomStr variable is an MutableDictionary. Thats why its not working.
here is my Test:
NSString *foo = #"BAR;FOO;VAR";
NSArray *arr = [foo componentsSeparatedByString:#";"];
NSLog(#"%#", arr);
Thise Logs:
BAR,
FOO,
VAR
EDIT:
Dictionary to array:
NSMutableDictionary *dict = #{#"aKey1" :#"BAR",
#"aKey2" :#"FOO",
#"aKey3" :#"VAR"}.mutableCopy;
NSMutableArray *array = [[NSMutableArray alloc] init];
for (NSString *key in dict) {
[array addObject:dict[key]];
}
NSLog(#"%#",array);
This Logs:
BAR,
FOO,
VAR

Adding a value and key into an NSMutableArray - Objc

I've read and tried a dozen or more variants of my own question, but still need some help please.
I have a large existing array, and I want to add a new object (key and value) to each record.
This is an element in the incoming array:
{
"trip_id": 65,
"arrival_time": "08:56:08",
"departure_time": "08:56:08",
"stop_id": 1161,
"stop_sequence": 8,
"stop_headsign": 0
},
This is what I want to achieve:
{
"trip_id": 65,
"arrival_time": "08:56:08",
"departure_time": "08:56:08",
"stop_id": 1161,
"stop_name": "a stop name",
"stop_sequence": 8,
"stop_headsign": 0
},
This is my code so far -- the commented lines are other attempts:
NSString *nameKey = #"stop_name";
int i=0;
for (i=0; i<stopTimesArray.count; i++) {
NSNumber *stopTimesId = [stopTimesArray[i] valueForKey:#"stop_id"];
int j=0;
for (j=0; j<stopArray.count; j++) {
NSNumber *stopId = [stopArray[j] valueForKey:#"stop_id"];
if (stopId == stopTimesId) {
NSString *stopNameString = [stopArray[j] valueForKey:#"stop_name"];
NSLog(#"stopNameString: %#", stopNameString);
[outgoingStopTimesDictionary setObject:stopNameString forKey:#"stop_name"];
//[outgoingStopTimesArray addObject:outgoingStopTimesDictionary];
//[outgoingStopTimesArray addObjectsFromArray:stopTimesArray[i]];
//[stopTimesArray[i] addObject:#{#"stop_name":stopNameString}];
//[stopTimesArray[i] addObject:#{#"stop_name":stopNameString}];
[stopTimesArray[i] addObject: outgoingStopTimesDictionary];
}
}
}
//NSLog(#"outgoingStopTimesArray: %#", outgoingStopTimesArray);
//NSLog(#"outgoingStopTimesDictionary: %#", outgoingStopTimesDictionary);
//NSLog(#"stopTimesArray: %#", stopTimesArray);
The error I am getting with approach is:
stopNameString: S Monroe Street, NB # 18th Street S, NS
[__NSCFDictionary addObject:]: unrecognized selector sent to instance 0x7fd7f2c22760
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary addObject:]: unrecognized selector sent to instance 0x7fd7f2c22760'
I'm either getting a null dictionary, or an unrecognised object exception when I try to add the dictionary to my array. Please point me to a working answer, and I'll delete my question.
It appears that your goal is to add one new key/value pair to the dictionary at stopTimesArray[i]. Here's your code all cleaned up with what I believe you need:
for (NSMutableDictionary *stopTimesDictionary in stopTimesArray) {
NSNumber *stopTimesId = stopTimesDictionary[#"stop_id"];
for (NSDictionary *stopDictionary in stopArray) {
NSNumber *stopId = stopDictionary[#"stop_id"];
if ([stopTimesId isEqual:stopId]) {
NSString *stopNameString = stopDictionary[#"stop_name"];
stopTimesDictionary[#"stop_name"] = stopNameString;
// Uncomment the following line if "stop_id" is unique within the "stopArray"
// break;
}
}
}
Since it seems your stopTimesArray contains immutable dictionaries, the above code won't work as written. Here is a solution that deals with that:
for (NSInteger i = 0; i < stopTimesArray.count; i++) {
NSDictionary *stopTimeDictionary = stopTimesArray[i];
NSNumber *stopTimesId = stopTimesDictionary[#"stop_id"];
for (NSDictionary *stopDictionary in stopArray) {
NSNumber *stopId = stopDictionary[#"stop_id"];
if ([stopTimesId isEqual:stopId]) {
NSString *stopNameString = stopDictionary[#"stop_name"];
NSMutableDictionary *tempDict = [stopTimesDictionary mutableCopy];
tempDict[#"stop_name"] = stopNameString;
[stopTimesArray replaceObjectAtIndex:i withObject:tempDict];
// Uncomment the following line if "stop_id" is unique within the "stopArray"
// break;
}
}
}
dictTo=[NSDictionary new];
dictTo =
#{
#"vPLocationLat" : [NSString stringWithFormat:#"%#",[[[[json valueForKey:#"result"] valueForKey:#"geometry"] valueForKey:#"location"] valueForKey:#"lat"]],
#"vPLocationLong" : [NSString stringWithFormat:#"%#",[[[[json valueForKey:#"result"] valueForKey:#"geometry"] valueForKey:#"location"] valueForKey:#"lng"]]
};
arrLocationList =[NSMutableArray new];
arrLocationList =[dictTo[#"data"] mutableCopy];
another Solution
-> Try to implement your Code as per in given Code
NSMutableArray* newArray = [NSMutableArray array];
NSArray* oldArray = outerDictionary[#"scores"];
for (NSDictionary* dictEntry in oldArray) {
NSString* leagueCode = dictEntry[#"league_code"];
if ([leagueCode isEqualToString #"epl"]) {
[newArray addObject:dictEntry];
}
}
Another one Solution
Try something like this.
Assume your array is called array and yourNewNameString is your new value for name
for(NSMutableDictionary *dict in array){
if([[dict objectForKey:#"id"]integerValue]==5){
[dict setObject:yourNewNameString forKey#"name"];
}
}
edit This is assuming you initialized your array with NSMutableDictionarys (Not just NSDictionarys)
//You can create dictionary and add it into NSMutableArray Object Like..
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSDictionary *inventory = #{
#"Mercedes-Benz SLK250" : [NSNumber numberWithInt:13],
#"Mercedes-Benz E350" : [NSNumber numberWithInt:22],
#"BMW M3 Coupe" : [NSNumber numberWithInt:19],
#"BMW X6" : [NSNumber numberWithInt:16],
};
[arr addObject:inventory];
//You can access using key like...
NSString *strForBMWX6 = [[arr objectAtIndex:0] valueForKey:#"BMW X6"];
// in your case you just miss objectAtIndex:j
First, thanks #rmaddy.
I modified his answer a bit, but his was basically correct.
My final code looks like this:
for (NSInteger i = 0; i < stopTimesArray.count; i++) {
NSDictionary *stopTimesDictionary = stopTimesArray[i];
NSNumber *stopTimesId = stopTimesDictionary[#"stop_id"];
for (NSDictionary *stopDictionary in stopArray) {
NSNumber *stopId = stopDictionary[#"stop_id"];
if ([stopTimesId isEqual:stopId]) {
NSString *stopNameString = stopDictionary[#"stop_name"];
NSMutableDictionary *tempDict = [stopTimesDictionary mutableCopy];
tempDict[#"stop_name"] = stopNameString;
[outgoingStopTimesArray addObject:tempDict];
break;
}
}
}

Put multiple arrays in Dictionary

I am parsing a CSV file multiple times with for loop, here I need to store these arrays one by one dictionary. There are very less questions in stack about adding NSArray to NSDictionary. I am parsing CSV with below code but I strucked at storing in NSDictionary, The program is terminating and showing warning at assigning string to dictionary
for (i=0; i<=57; i++) {
NSString *keysString = [csvArray objectAtIndex:i];
NSArray *keysArray = [keysString componentsSeparatedByString:#","];
NSLog(#"Serail No %d %#",i,keysArray);
NSString *string = [NSString stringWithFormat:#"%d", i];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjects: keysArray forKeys: string];
}
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
for (i=0; i<=57; i++) {
NSString *keysString = [csvArray objectAtIndex:i];
NSArray *keysArray = [keysString componentsSeparatedByString:#","];
NSString *key = [NSString stringWithFormat:#"serial%d",i];
[dict setObject:keysArray forKey:key];
}
To get back data from dictionary,
NSArray *array = [dict valueForKey:#"serial24"];//to get array 24.
If I understand you correctly, you want to add the arrays to a dictionary, with the key being the string value of integer i ? What you need to do is allocate the dictionary outside your loop -
NSMutableDictionary *dict=[NSMutableDictionary new];
for (i=0; i<=57; i++) {
NSString *keysString = [csvArray objectAtIndex:i];
NSArray *keysArray = [keysString componentsSeparatedByString:#","];
NSLog(#"Serial No %d %#",i,keysArray);
NSString *string = [NSString stringWithFormat:#"%d", i];
dict[string]=keysArray;
}
I am not sure why you would want to do this, because this is basically an array. You could simply do -
NSMutableArray *outputArray=[NSMutableArray new];
for (NSString *keysString in csvArray) {
NSArray *keysArray = [keysString componentsSeparatedByString:#","];
[outputArray addObject:keysArray];
}

Unable to extracting a substring from an NSString

I have an input string in the format
"Jerry Lane"(angle bracket)jerry.lane#gmail.com(bracket closed),"Harry Potter"(angle bracket)harry.potter#gmail.com(bracket closed),"Indiana Jones",(angle bracket)indiana.jones#gmail.com(bracket closed),"Tom Cruise"(angle bracket)tom.cruise#gmail.com(bracket closed)
Here, i am supposed to first separate the string on the basis of comma delimiter, which would give me a separate string like
"Jerry Lane"(angle bracket)jerry.lane#gmail.com(bracket closed)
Then i need to save extract the string between the <> brackets, which is essentially the string "jerry.lane#gmail.com". I am using the following code, but it is giving me the following error:
Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFConstantString substringWithRange:]: Range or index out of bounds'
-(NSArray *)parseString:(NSString *)string
{
if(string)
{
NSArray *myArray = [string componentsSeparatedByString:#","];
for(NSMutableString *myString in myArray)
{
NSRange start,end;
start = [myString rangeOfString:#"<"];
end = [myString rangeOfString:#">"];
if(start.location != NSNotFound && end.location != NSNotFound)
{
NSString *emailAddress = [myString substringWithRange:NSMakeRange(start.location,end.location)];
NSString *name = [myString substringToIndex:start.location];
NSDictionary *myDictionary = [[NSDictionary alloc] init];
[myDictionary setValue:emailAddress forKey:#"Dhruvil Vyas"];
[testArray addObject:myDictionary];
}
}
}
return testArray;
}
The arguments that substring takes are the start position and the length
Not the start position and the end position.
More Info
borrrden's answer is correct. Here is another way to do this.
-(NSArray *)parseString:(NSString *)string
{
if(string)
{
NSArray *myArray = [string componentsSeparatedByString:#","];
for(NSMutableString *myString in myArray)
{
NSArray *tempNameArray = [myString componentsSeparatedByString:#"<"];
NSString *email = [tempNameArray objectAtIndex:1];
NSArray *tempMailArray = [email componentsSeparatedByString:#">"];
NSString *emailAddress = [tempMailArray objectAtIndex:0];
NSString *name = [tempNameArray objectAtIndex:0];
NSDictionary *myDictionary = [[NSDictionary alloc] init];
[myDictionary setValue:emailAddress forKey:#"Dhruvil Vyas"];
[testArray addObject:myDictionary];
}
}
return testArray;
}

Resources