how to check String value index in NSMutableArray - nsstring

NSMutableArray *arr_Status =[#"Pending",#"In Progress",#"In Testing",#"Needs Research",#"Limitation",#"Issue Not Clear",#"Unassigned"];
I am Having one NSString *String = #"Limitation"
how to get index String Value in NSMutableArray

Related

Append String with NSURL

I have following string , which i want to append with NSURL and after appending i want the result in NSURL
{ "deviceid":"3c27c99ac4b159aca81de8f5d266478f00000000 ","nickname":"sad","gender":0,"marital":0,"children":1,"job":"asd","message":"Asd","pushid":"3c27c99ac4b159aca81de8f5d266478f00000000"}
Can , Anybody help me please .
Thanks in advance .
You haven't stated what you expect the final URL to look like, so I have assumed you want to add the names and values from your record string as a query string to the original URL.
The following method will return a combined URL when given a base URL and string like the one you have provided above:
-(NSURL *)URLWithRecord:(NSString *)record relativeToURL:(NSURL *)originalURL
{
NSCharacterSet * unwantedDelimeters = [NSCharacterSet characterSetWithCharactersInString:#"{}"];
NSCharacterSet * fieldSeperator = [NSCharacterSet characterSetWithCharactersInString:#","];
NSCharacterSet * nameValueSeperator = [NSCharacterSet characterSetWithCharactersInString:#":"];
NSCharacterSet * quotes = [NSCharacterSet characterSetWithCharactersInString:#"\""];
record = [record stringByTrimmingCharactersInSet:unwantedDelimeters];
NSArray * fields = [record componentsSeparatedByCharactersInSet:fieldSeperator];
NSMutableString * queryString = [NSMutableString stringWithString:#"?"];
for (NSUInteger fieldCount = 0; fieldCount < [fields count]; fieldCount++) {
NSString * field = [fields objectAtIndex:fieldCount];
NSArray * nameValue = [field componentsSeparatedByCharactersInSet:nameValueSeperator];
NSString * name = [[nameValue objectAtIndex:0] stringByTrimmingCharactersInSet:quotes];
NSString * value = [[nameValue objectAtIndex:1] stringByTrimmingCharactersInSet:quotes];
if (fieldCount == ([fields count]-1) ) {
[queryString appendFormat:#"%#=%#", name, value];
} else {
[queryString appendFormat:#"%#=%#&", name, value];
}
}
NSURL * combinedURL = [NSURL URLWithString:queryString relativeToURL:originalURL];
return combinedURL;
}
When tested with the following code:
NSURL * originalURL = [NSURL URLWithString:#"http://www.example.com"];
NSString * string = #"{\"deviceid\":\"3c27c99ac4b159aca81de8f5d266478f00000000\",\"nickname\":\"sad\",\"gender\":0,\"marital\":0,\"children\":1,\"job\":\"asd\",\"message\":\"Asd\",\"pushid\":\"3c27c99ac4b159aca81de8f5d266478f00000000\"}";
NSURL * combinedURL = [self URLWithRecord:string relativeToURL:originalURL];
NSLog(#"result=\"%#\"", [combinedURL absoluteString]);
The output is:
result="http://www.example.com?deviceid=3c27c99ac4b159aca81de8f5d266478f00000000&nickname=sad&gender=0&marital=0&children=1&job=asd&message=Asd&pushid=3c27c99ac4b159aca81de8f5d266478f00000000"
The method provided assumes that there are no erroneous spaces in the record string and that the names and values in the record only contain ASCII numbers and letters. It will return a nil value if the record contains names or values that contain URL problem characters (such as a space). If you suspect that such characters will be involved, you will need to rewrite the method accordingly - replacing such characters with URL escape codes.
NSString *str=#"";
NSString *str1=#"\"deviceid\":\"3c27c99ac4b159aca81de8f5d266478f00000000 \",\"nickname\":\"sad\",\"gender\":0,\"marital\":0,\"children\":1,\"job:\"asd\",\"message\":\"Asd\",\"pushid\":\"3c27c99ac4b159aca81de8f5d266478f00000000\"";
NSURL *url=[NSURL URLWithString:#"give your url"];
NSArray *components = [url pathComponents];
for (NSString *c in components)
{
str=[str stringByAppendingString:c];
}
str=[str stringByAppendingString:str1];
NSURL *newurl=[NSURL URLWithString:#"str"];

How to store images and keys to NSMutableDictionary

I'm receiving an error when writing to my NSMutableDictionary "dictionary".
When implementing (below) from my item list class:
- (void)setImage:(UIImage *)i forKey:(NSString *)s
{
[dictionary setObject:i forKey:s];
// Create full path for image
NSString *imagePath = [self imagePathForKey:s];
// Turn image into JPEG data,
NSData *d = UIImageJPEGRepresentation(i, 0.5);
// Write it to full path
[d writeToFile:imagePath atomically:YES];
}
From DetailViewController:imagePickerController:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *oldKey = [_detailItem imageKey];
//did the item already have an image
if (oldKey) {
//delete old image
[[RoomList sharedStore] deleteImageForKey:oldKey];
}
//get picked image from info dictionary
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//create CFUUID object; it knows how to create uique identifier strings
CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);
//create string from unique identifier
CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID);
//use unique ID to set our item's imageKey
NSString *key = (__bridge NSString *)newUniqueIDString;
[_detailItem setImageKey:key];
NSLog(#"image key: %#", key);
[_detailItem setImage:image forKey:key];
//tell objects ointed to by newUniqueIDString and newUniqueID to lose an owner to avoid
//memory leak
CFRelease(newUniqueIDString);
CFRelease(newUniqueID);
//remove image picker (dismiss method)
[self dismissViewControllerAnimated:YES completion:nil];
[self configureView];
}
I am being met with the following error, that is tied to the [_detailItem setImage:image forKey:key]; operation in imagePickerController: -[NSManagedObject setImage:forKey:]: unrecognized selector sent to instance 0x8195770
My guess is that either the UIImage *image, NSString *key, or both are not able to be passed to the objectForKey method "setImage:forKey". Any way of fixing this, or getting around it?
Thanks!

Converting nsarray to nsdata to nsstring to use in label

Help from the more experienced Obj C coders will be greatly appreciated as I've been stuck on this for a few days, and I believe the code is 'close'.
I'm creating a list of names from the users contacts, then I pass this list back to a label. My problem (I think) is getting from the NSData format I'm using to archive the list with NSKeyedArchiver into an NSString. I've read everything I can find, but I suspect my being a NOOB is holding me back from deciphering some other example.
Here is the part of my *.m file where I create a list of persons into an array called "_objects", then I archive the "_objects" (NSKeyArchiver) making an NSData (data) which I then try to create a NSString (guestListString) from. It seems to all go well, except the "guestListString" has a bunch of extra gobblygook characters (yeah, a high tech term) included all around my list of person names. I've tried other formats, but they either return (null) or a bunch of foriegn looking characters. I suspect I've not learned some step I need to insert to get rid of these 'gobbly gook' characters around my list of comma separated names.
Here is the code:
// now grab the 'person' property from the Addressbook, pass it as a string back to 'tableview cell', inserting it at 'row 0' and update display
- (void)displayPerson:(ABRecordRef)person {
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
NSString *selectedPerson = (__bridge NSString *)ABRecordCopyCompositeName(person);
//NSString *firstname =(__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);
//NSString *lastname =(__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonLastNameProperty);
#ifdef DEBUG
NSLog(#"displayPerson insertRowsAtIndexPath selectedPerson = "#"%#",selectedPerson); // shows last added name
#endif
[_objects insertObject:selectedPerson atIndex:0];
//[_objects insertObject:firstname atIndex:0];
//[_objects insertObject:lastname atIndex:0];
#ifdef DEBUG
NSLog(#"displayPerson insertRowsAtIndexPath _objects = "#"%#",_objects); // shows 'list' of all names currently in list
//NSLog(#"displayPerson insertRowsAtIndexPath stringWithFormat _objects = "#"%#",[NSString stringWithFormat:#"%#",_objects]);
#endif
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//
// (Code to Archive an array) Given that "_objects" contains an array of 'selectedPerson' objects
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_objects];
// Now save to NSUser defaults
[[NSUserDefaults standardUserDefaults] setObject:data forKey:#"_objects"];
// Now let's see what is in 'data'
NSLog(#"archive of 'data' from key _objects = "#"%#",data); // shows long list of 8 bit numbers
//
// (Code to unarchive an array) Now to unarchive:
// I'm doing this code example here just so I understand unarchive procedure; not needed in actual app code
NSData *_objectsData = [[NSUserDefaults standardUserDefaults] objectForKey:#"_objects"];
NSArray *backIntoArray = [NSKeyedUnarchiver unarchiveObjectWithData:_objectsData];
//
// Now lets see what is in "backIntoArray"
NSLog(#"unarchive of _objectsData = "#"%#",_objectsData); // shows long list of 8 bit numbers
NSLog(#"unarchive of NSArray to backIntoArray = "#"%#",backIntoArray); // shows 'list' of all names currently in list
//
// Now convert NSData (data) to NSString so I can pass it to other elecments like 'labels'
//NSString *guestListString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; // this is equivalent code to next two lines
NSString *guestListString;
guestListString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; // returns extra characters around list of names instead of list of names ???
// other potential format options that could exist
//guestListString = [[NSString alloc] initWithData:data encoding:NSNEXTSTEPStringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSJapaneseEUCStringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSNEXTSTEPStringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // returns (null) ???
//guestListString = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSSymbolStringEncoding]; // returns (null) ???
//guestListString = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding]; // returns (null) ???
//guestListString = [[NSString alloc] initWithData:data encoding:NSShiftJISStringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSISOLatin2StringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSUnicodeStringEncoding]; // returns string of japanese looking characters???
//guestListString = [[NSString alloc] initWithData:data encoding:NSWindowsCP1251StringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSWindowsCP1252StringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSWindowsCP1253StringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSWindowsCP1254StringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSWindowsCP1250StringEncoding]; // returns (null) ???
//guestListString = [[NSString alloc] initWithData:data encoding:NSISO2022JPStringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSMacOSRomanStringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSUTF16StringEncoding]; // returns string of japanese looking characters???
//guestListString = [[NSString alloc] initWithData:data encoding:NSUTF16BigEndianStringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSUTF16LittleEndianStringEncoding]; // returns string of japanese looking characters???
//guestListString = [[NSString alloc] initWithData:data encoding:NSUTF32StringEncoding]; // returns (null) ???
//guestListString = [[NSString alloc] initWithData:data encoding:NSUTF32BigEndianStringEncoding]; // returns (null) ???
//guestListString = [[NSString alloc] initWithData:data encoding:NSUTF32LittleEndianStringEncoding]; // returns (null) ???
//guestListString = [[NSString alloc] initWithData:data encoding:NSProprietaryStringEncoding]; // shows
//
// show me what is stored in NSString 'guestListString'
NSLog(#"displayPerson NSString conversion of 'data' to 'guestListString' = "#"%#",guestListString); //
//
// Pass the required text back to 'guestsListLabel.text' on ViewController screen
//((InitialViewController *)self.presentingViewController).guestListLabel.text=selectedPerson; // This WORKS passing back the last selectedPerson 1 name
((InitialViewController *)self.presentingViewController).guestListLabel.text=guestListString; // Not working yet; trying to pass back full list of names
//
// add code here to handle saving of guestlist before leavig this 'insertRowsAtIndexPath' section
// Archiving is simple, using the following code:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// This next line archives objects correctly so they can be reloaded later and be editable (mutable)
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:_objects] forKey:#"guestListTable"];
// This next line writes the value loaded into 'guestListLabel.text' into variable 'kGuestListText' which is used in InitialViewController to display in all 3 field types
[userDefaults setValue:((InitialViewController *)self.presentingViewController).guestListLabel.text forKey:kGuestsListText];
// update with all userDefaults variables
[userDefaults synchronize];
//
}

plist - NSMutableDictionary - 0x0

I have a problem:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Settings" ofType:#"bundle"];
NSString *settingPath = [[[NSString alloc] init] autorelease];
settingPath = [[NSBundle bundleWithPath:filePath] pathForResource:#"Root" ofType:#"plist"];
NSMutableDictionary *plist = [[[NSMutableDictionary alloc] init] autorelease];
plist = [NSMutableDictionary dictionaryWithContentsOfFile:settingPath];
after this plist is nil, adress ist 0x0 ... but why ?
Can anyone help me ?
Below is some code that should work OK:
NSString* path = [[NSBundle mainBundle] pathForResource:#"myfile" ofType:#"plist"];
NSDictionary* dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
Note that the plist must be valid, otherwise you will get a null value.
Also, note that you don't need to use alloc/init to create a variable if you're just going to overwrite its value in another call.

reading unicode from sqlite and creating a NSString

Im working on an iOS app where I need to store and retrieve from an SQLite DB, a representation of a NSString that has subscripts. I can create a NSString at compile time with a constant:
#"Br\u2082_CCl\u2084"
\u2082 is a 2 subscript, \u2084 is a 4 subscript. What im storing in the SQLite db is:
"Br\u2082_CCl\u2084"
But what I can not figure out how to do, is reconvert that back into an NSString. The data comes back from the db as a char * "Br\\u2082_CCl\\u2084" Stripping out the extra slash has made no difference in my feeble experiments. I need a way get that back into an NSString - Thanks!
You need one of the NSString stringWithCString class methods or the corresponding initWithCString instance methods.
I solved the problem like this - error checking removed for clarity -
the unicode string comes into the parameter stringEncoded from the db like:
"MgBr\u2082_CH\u2082Cl\u2082"
+(NSString *)decodeUnicodeBytes:(char *)stringEncoded {
unsigned int unicodeValue;
char *p, buff[5];
NSMutableString *theString;
NSString *hexString;
NSScanner *pScanner;
theString = [[[NSMutableString alloc] init] autorelease];
p = stringEncoded;
buff[4] = 0x00;
while (*p != 0x00) {
if (*p == '\\') {
p++;
if (*p == 'u') {
memmove(buff, ++p, 4);
hexString = [NSString stringWithUTF8String:buff];
pScanner = [NSScanner scannerWithString: hexString];
[pScanner scanHexInt: &unicodeValue];
[theString appendFormat:#"%C", unicodeValue];
p += 4;
continue;
}
}
[theString appendFormat:#"%c", *p];
p++;
}
return [NSString stringWithString:theString];
}

Resources