Error adding NSString variable into plist file - nsstring

I've a problem I'm struggling with for a lot of time...please help.
I'm using the following line of code to add record to a plist file:
[data_appo setObject:#"string value" forKey:[NSString stringWithFormat:#"%i",(i-1)]];
and it works ok. Now,if I try to read the string from the file than adding it (I need to duplicate the value with a different key) as follows
NSString *string_appo=[data_appo objectForKey:[NSString stringWithFormat:#"%i",j]];
[data_appo setObject:string_appo forKey:[NSString stringWithFormat:#"%i",(i-1)]];
then I gest ERROR_EXEC and the app crashes...
Any idea???
Any help will be appreciated
Thanks in advance!

Have you saved the plist file after making the addition. I do this a lot and I do it like this
//find the plist file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Notes.plist"];
//set the value for the key
[notesData setValue:textView.text forKey:[NSString stringWithFormat:#"General notes %d",selectedIndex]];
//save the plist file
[notesData writeToFile: path atomically:YES];
//relese and reload it
[notesData release];
notesData = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
I hope this helps!

Related

watchkit WKAlertAction openSystemURL

I am trying to show the User different Phone Numbers on Apple Watch and he clicks on one than phone call alert should appear. I'll do it like this but the Alert is just dismissed without call action:
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:0];
WKExtension *myExt = [WKExtension sharedExtension];
for (NSString *phone in arr) {
NSString *tel = [NSString stringWithFormat:#"tel:%#",phone];
WKAlertAction *act = [WKAlertAction actionWithTitle:tel style:WKAlertActionStyleDefault handler:^(void){
[myExt openSystemURL:[NSURL URLWithString:phone1]];
}];
[tempArray addObject:act];
}
NSString *titleMessage = #"Call";
NSString *textMessage = #"Please select the number you want to call.";
NSString *cancel = #"Cancel";
WKAlertAction *act = [WKAlertAction actionWithTitle:cancel style:WKAlertActionStyleDestructive handler:^(void){
}];
[tempArray addObject:act];
[self presentAlertControllerWithTitle:titleMessage message:textMessage preferredStyle:WKAlertControllerStyleAlert actions:tempArray];
Buttons are shown as expected and the Handler is also called with the correct Phone Number. But it does not openSystemURL. Does somebody know why and how to fix? Thanks!
I think you forgot to add "tel" scheme ,Use below code :
[WKAlertAction actionWithTitle:#"tel" style:WKAlertActionStyleDefault handler:^(void){
[[WKExtension sharedExtension]openSystemURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel:%#",#"YOUR NUMBER"]]];
}];
About Apple URL Schemes

NSString with HTML code to NSData

Thanks in advance!
I'm wondering if it's possible to convert HTML code stored in a NSString to a NSData to parse later.
I'm reading from a BBDD the HTML code and saving it into a NSString.
NSString *htmlString =#"<html><body><p>introduccion</p><p>introducción</p></body></html>";
I want to use:
NSData *nsData = [[NSData alloc] initWithContentsOfURL:url];
But instead of initWithContentsOfURL i have to use the htmlString because I have the code stored in a BBDD and I am accessing it and storing the code to a NSString
Thanks!
Why dont you use standard methods to convert NSString to NSData like this:
NSData* data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
I have some html formatted text in my "subject.details", I add some more html formatting on the front and back. Giving me a "detailsStringForDisplay" NSString.
I convert this string to an NSData.
Set up a dictionary, which only contains one element to say that the document type is HTML.
Then finally set an attributedText using the data and the dictionary.
NSString *fontDetailsHTML = #"<div style=\"font-size:17px; text-family:Georgia; \">";
NSString *detailsString = subject.details;
NSString *endFontDetailsHTML = #"</div>";
NSString *detailsStringForDisplay = [NSString stringWithFormat:#"%#%#%#",fontDetailsHTML,detailsString,endFontDetailsHTML];
NSData *labelTextData = [detailsStringForDisplay dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:true];
NSDictionary *attributesForText = [[NSDictionary alloc]initWithObjectsAndKeys:NSHTMLTextDocumentType,NSDocumentTypeDocumentAttribute , nil];
self.detailDescriptionLabel.attributedText = [[NSAttributedString alloc]initWithData:labelTextData options:attributesForText documentAttributes:nil error:nil];

Declaring an NSDictionary with an NSString as its name

Ok, so I would like to create an NSDictionary, but the pointer should be followed by the value of an NSString that has been put in by the user. Is this possible? I imagine it would be something along the lines of this...
someNSString = _someTextField.text;
NSDictionary * {someNSString} = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:someUnimportantVariable], #"someUnimportantKey",...nil]
Thanks in advance, I realize that this is either completely not possible or there is a pretty simple solution. Either way, I'm sorry if I wasted your time.
you could try creating a "dictionary of dictionaries". something to the effect of:
someNSString = _someTextField.text;
NSDictionary *myDictionary = [[NSDictionary alloc] init];
[myDictionary setObject:[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:someUnimportantVariable], #"someUnimportantKey",...nil] forKey:someNSString];
and if you wanted to get back that dictionary
NSDictionary someNSStringDictionary = [myDictionary objectForKey:someNSString];
let me know if it helps in any way.

Best way to combine string and placemark data for NSUrl?

I'm trying to get the temp and weather data for my app based on the latitude and longitude location...
How can I get this to work?
NSURL *jsonurl = [NSURL URLWithString:#"http://www.myweather2.com/developer/forecast.ashx?uac=(API-KEY)&output=json&query=",placemark.location,#"&temp_unit=f"];
NSString *jsonStr = [NSString stringWithFormat:#"http://www.myweather2.com/developer/forecast.ashx?uac=(API-KEY)&output=json&query=%#&temp_unit=f", placemark.location];
NSURL *jsonurl = [NSURL URLWithString:jsonStr];
This assumes that the value for placemark.location is an NSString that only contains valid URL characters.

Trim file extension UITableView

I want to trim the file extension from text I have an NSMutableArray in table cells.
NSMutableArray *theFiles = [NSMutableArray new];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager directoryContentsAtPath:#"/Test"];
for (NSString *s in fileList){
[theFiles addObject:fileList];
}
cell.textLabel.text = theFiles[indexPath.row];
return cell;
This lists for example "Xylophone.m4r" I want to remove the .m4r.
Try -[NSString stringByDeletingPathExtension] (in NSPathUtilities.h).
Actually, for my use, I was able to create a plist programmatically and just not use an extension and it works great! However, anothe rway to do this is:
[string stringByReplacingOccurrencesOfString:#".fileextension" withString:#""];
Just have to delete path extension component:
cell.textLabel.text = [[theFiles objectAtIndex:indexPath.row] stringByDeletingPathExtension];

Resources