I have an app where the user can choose an image either from the built-in app images or from the iphone photo library. I use an object Occasion that has an NSString property to save the imagePath.
Now in the case of the built-in app images I do get the file name as an NSString an save in the [occasion imagePath]. But in the 2nd case where the user picks an image form the photo library I get an NSURL which I want to convert to an NSString to be able to save it in [occasion imagePath].
Is it possible to convert the NSURL to an NSString?
In Objective-C:
NSString *myString = myURL.absoluteString;
In Swift:
var myString = myURL.absoluteString
More info in the docs:
If you're interested in the pure string:
[myUrl absoluteString];
If you're interested in the path represented by the URL (and to be used with NSFileManager methods for example):
[myUrl path];
Try this in Swift :
var urlString = myUrl.absoluteString
Objective-C:
NSString *urlString = [myURL absoluteString];
Swift update:
var myUrlStr : String = myUrl.absoluteString
I just fought with this very thing and this update didn't work.
This eventually did in Swift:
let myUrlStr : String = myUrl!.relativePath!
You can use any one way
NSString *string=[NSString stringWithFormat:#"%#",url1];
or
NSString *str=[url1 absoluteString];
NSLog(#"string :: %#",string);
string :: file:///var/containers/Bundle/Application/E2D7570B-D5A6-45A0-8EAAA1F7476071FE/RemoDuplicateMedia.app/loading_circle_animation.gif
NSLog(#"str :: %#", str);
str :: file:///var/containers/Bundle/Application/E2D7570B-D5A6-45A0-8EAA-A1F7476071FE/RemoDuplicateMedia.app/loading_circle_animation.gif
In Swift :-
var str_url = yourUrl.absoluteString
It will result a url in string.
Related
I would like to extract the id number part from an app store url, for example, following are two app store urls:
https://itunes.apple.com/app/apple-store/id882456583?pt=63826800&ct=%E5%AE%A3%E4%BC%A0%E8%B4%B4001&mt=8
https://itunes.apple.com/app/juan-pi-zhe-kou-shou-ji-shang/id639388447?mt=8&uo=4
I would like to extract the number after "id", i.e. 882456583 and 639388447.
Anyone knows how to do this? Thank you.
Try this
Swift :
let appStoreUrl = NSURL(string: "https://itunes.apple.com/app/juan-pi-zhe-kou-shou-ji-shang/id639388447?mt=8&uo=")
let appId = appStoreUrl?.lastPathComponent?.stringByReplacingOccurrencesOfString("id", withString: "")
Objective C :
NSURL *appStoreUrl = [NSURL URLWithString:#"https://itunes.apple.com/app/juan-pi-zhe-kou-shou-ji-shang/id639388447?mt=8&uo="];
NSString *appId = [appStoreUrl.lastPathComponent stringByReplacingOccurrencesOfString:#"id" withString:#""];
Result:
What you are expecting is this
NSURLComponents *components = [NSURLComponents componentsWithURL:[NSURL URLWithString:#"https://itunes.apple.com/app/apple-store/id882456583?pt=63826800&ct=%E5%AE%A3%E4%BC%A0%E8%B4%B4001&mt=8"] resolvingAgainstBaseURL:NO];
NSLog(#"id = %#",[[[components valueForKey:#"path"] componentsSeparatedByString:#"/"] lastObject]);
in logs you ll have required param.
What I am looking to do is use placeholders in a string and replace the placeholders with data specific to the user. I can setup the placeholders to be anything so basically I am looking to do the following:
Setup placeholders in a string (up to 4 placeholders)
Replace those placeholders with strings I specify
Here is what I have. I currently have a url that has a set of placeholders like so. http://example.com/resource?placeholder1=placeholder2 or http://placeholder1:placeholder2#example.com/something?placeholder3
How do I properly label the placeholders and replace them?
Thank you in advance for any help.
You can use the stringByReplacingOccurrencesOfString method as below
NSString *strUrl = #"http://example.com/resource?placeholder1=placeholder2";
strUrl = [strUrl stringByReplacingOccurrencesOfString:#"placeholder1" withString:#"value1"];
strUrl = [strUrl stringByReplacingOccurrencesOfString:#"placeholder2" withString:#"key1"];
Quote : "I want to replace placeholder1 with an NSString I have already created called value1 and placeholder2 with an NSString called key1."
NSString *mainUrl = "http://example.com/resource";
NSString *string1 = "value1";
NSString *string2 = "value2";
Now change your URL:
NSString *newURL = [NSString NSStringWithFormat:#"%#?%#=%#",mainUrl,string1,string2];
This will generate newURL : http://example.com/resource?value1=value2
This might help you.
#define placeHolder1 #"<>p1p1p1<>"
#define placeHolder2 #"<>p2p2p2<>"
And place this in a function of yours where you want to replace strings
NSString * string = [NSString stringWithFormat:#"http://example.com/resource?%#=%#",placeHolder1,placeHolder2];
NSLog(#"string %#",string);
NSString * replacerForP1 = #"123";
NSString * replacerForP2 = #"741";
string = [string stringByReplacingOccurrencesOfString:placeHolder1
withString:replacerForP1];
string = [string stringByReplacingOccurrencesOfString:placeHolder2
withString:replacerForP2];
NSLog(#"string %#",string);
It would be best to keep your placeholder strings in the constants defined somewhere. And ofcourse the replacement of those placeholders will be dynamic as you said so cannot make them constants.
Tell me if this helps or if you require further assistance in the matter.
I have a complete URL, say
http://www.mywebsite.com//Folder /Detals /Final Image /La Image Logo.jpg
Here in this NSString, I only want to fetch
La Image Logo.jpg
How can I do so?
Thanks
[string lastPathComponent] should do the trick
If it's about the filename in a URL you should definitely create an NSURL object and query that object for the desired information:
NSURL *url = [NSURL URLWithString:#"http://www.mywebsite.com//Folder /Detals /Final Image /La Image Logo.jpg"];
NSString *filename = [url lastPathComponent];
NSURL is good at parsing URL strings. This way you're sure that you don't get any fragment part ("#anchor") or parameters ("?q=foo&p=bar") in your filename.
You can use NSString *string=[[yourString componentsSeparatedByString:#"/"] lastObject]
Hello everyone I am trying find a string inside a string
lets say I have a string:
word1/word2/word3
I want to find the word from the end of the string to the last "/"
so what I will get from that string is:
Word3
How do I do that?
Thanks!
You are looking for the componentsSeparatedByString: method
NSString *originalString = #"word1/word2/word3";
NSArray *separatedArray = [originalString componentsSeparatedByString:#"/"];
NSString *lastObject = [separatedArray lastObject]; //word3
once check this one By using this one you'l get last pathcomponent values,
NSString* theFileName = #"how /are / you ";
NSString *str1=[theFileName lastPathComponent];
NSLog(#"%#",str1);
By using lastPathComponent you'l get the last path component directly no need to take array for separate the string.
you must use NSScanner class to split substring.
check this.
Objective C: How to extract part of a String (e.g. start with '#')
NSString *string = #"word1/word2/word3"
NSArray *arr = [string componentsSeperatedByString:#"/"];
NSSting *str = [arr lastObject];
You can find it also with this way:
NSMutableString *string=[NSMutableString stringWithString:#"word1/word2/word3"];
NSRange range=[string rangeOfString:#"/" options:NSBackwardsSearch];
NSString *subString=[string substringFromIndex:range.location+1];
NSRegularExpression or NSString rangeOfString:options:range:locale: (with options to search backwards).
The answer really depends on exactly what the input string will contain (how consistent it is).
I'm trying to convert a URL to a String with the following code:
NSURL *urlOfOpenedFile = _service.myURLRequest.URL;
NSString *fileThatWasOpened = [NSString stringWithContentsOfURL:urlOfOpenedFile encoding:NSASCIIStringEncoding error:nil];
NSLog(#"url: %#", urlOfOpenedFile);
NSLog(#"string: %#", fileThatWasOpened);
This returns:
url: file://localhost/Users/User/Library/Application%2520Support/iPhone%2520Simulator/6.0/Applications/72AA03A1-3967-4F22-9745-0722C8DE9FAC/Documents/downloads/My%20Document%20.pdf
string: (null)
Can anyone tell me how to convert the URL to a string? Thanks.
You can use
NSString* yourURLString = [urlOfOpenedFile absoluteString];
The method you use is not for converting a NSURL to a NSString. Instead like the method name it returns the string from content of that URL. Since you have PDF file (not string) on that URL it returns nil.
For converting you can use the following instance method of NSURL
- (NSString *)absoluteString;
you can use like
NSString *fileThatWasOpened = [NSString stringWithFormat:#"%#", url];