Trim file extension UITableView - nsstring

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

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

Unable to append items in value array of mutable dictionary

NSMutableDictionary *mutableDict = [NSMutableDictionary dictionaryWithDictionary: #{ #"Maruti":#[#"m1",#"m2"], #"Hyundai":#[#"h1",#"h2"] } ];
How can I add values in value array of each key so that my output would look like:
Maruti:[m1,m2,m3] Hyundai:[h1,h2,h3]
Both Maruti and hyundai dictionary are NSDictionary so you cannot add any new values into them , plus you cannot use any of them outside of the scope of this method, but if you still want to do something like this you can go with this.
NSMutableDictionary * tempDict = [[NSMutableDictionary alloc]init];
[tempDict setValue:#"5" forKey:#"number"];
[tempDict setValue:#"6" forKey:#"Hidden"];
NSMutableDictionary *tempDict2 = [[NSMutableDictionary alloc]init];
[tempDict setValue:#"5" forKey:#"number"];
[tempDict setValue:#"6" forKey:#"Hidden"];
NSMutableDictionary *mutableDict=[[NSMutableDictionary alloc] init];
[mutableDict setValue:tempDict forKey:#"Dict 1"];
[mutableDict setValue:tempDict2 forKey:#"Dict 2"];

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

Error adding NSString variable into plist file

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!

Building an NSstring out of an NSMutableArray

My eyes hurt from hours of trying to figure this one - and i have looked for an answer for quite a while on-line (it will be embarrassing to tell how much...).
all i am trying to do is to enumerate using a for-in loop on anExpression which is a NSMutableArray that holds NSNumbers and NSStrings.
my NSLog print for the variable ans returns an empty string.
What am i doing wrong?
NSString *ans = #"";
for (id obj in anExpression)
{
if ([obj isKindOfClass:[NSString class]])
[ans stringByAppendingString:(NSString *)obj];
if ([obj isKindOfClass:[NSNumber class]])
[ans stringByAppendingString:(NSString *)[obj stringValue]];
NSLog(#"String so far: %# ", ans);
}
I think you mean
ans = [ans stringByAppendingString:(NSString *)obj];
not just
[ans stringByAppendingString:(NSString *)obj];
NSStrings are immutable -- you can't append to them. -stringByAppendingString: returns a new string (which you could then assign to ans).
Alternatively, you might use an NSMutableString and the -appendString: method.
Hey, sorry for the bad coding format, posting it again ...
NSString *ans = #"";
for (id obj in anExpression)
{
if ([obj isKindOfClass:[NSString class]])
[ans stringByAppendingString:(NSString *)obj];
if ([obj isKindOfClass:[NSNumber class]])
[ans stringByAppendingString:(NSString *)[obj stringValue]];
NSLog(#"String so far: %# ", ans);
}
[ans autorelease];
NSLog(#"final string is: %# ", ans);
return ans;
the method stringByAppendingString: returns a new string made by appending the given string to the receiver.
so you want ans = [ans stringByAppendingString:obj];

Resources