Convert an NSURL to an NSString
In Objective-C:
NSString *myString = myURL.absoluteString;
In Swift:
var myString = myURL.absoluteString
More info in the docs:
How to convert NSUrl to NSString?
Is it possible to convert NSUrl in to NSString for video file path.
Yes. Send it an absoluteString
message.
i have try to use NSString *path = [ExportoutputURL absoluteString]; but it crash.
If you want a path, send the URL a path
message. A string representing a URL is generally not a valid path; if you want a path, ask it for one.
As for the crash, it does not mean absoluteString
is wrong. Sending absoluteString
to an NSURL object is the correct way to get an NSString object that represents the URL. The problem is somewhere else.
Error EXC_BAD_ACCESS at
NSString *path = [ExportoutputURL absoluteString];
This probably means that ExportoutputURL
points to something that is not nil
but is also not a valid object. It might have pointed to an NSURL object at some point, but it doesn't now.
My guess would be that the problem is this:
ExportoutputURL = session.outputURL;
You assign the URL to the ExportoutputURL
instance variable, but you don't retain the object or make your own copy. Therefore, you don't own this object, which means you are not keeping it alive. It may die at any time, most probably after this method (exportDidFinish:
) returns.
The crash is because you call uploadVideoFile
later, after the URL object has already died. You still have a pointer to it, but that object no longer exists, so sending a message to it—any message—causes a crash.
There are three simple solutions:
- Retain the URL object when you assign it to your instance variable.
- Make your own copy of the URL object and assign that to the instance variable.
- Declare
ExportoutputURL
as a property, with either thestrong
keyword or thecopy
keyword, and assign the object to the property, not the instance variable. That will call the property's setter, which, if you synthesize it or implement it correctly, will retain or copy the URL for you.
Either way, you will own the object, and that will keep it alive until you release it. Accordingly, you will need to release it when you are done with it (in dealloc
, if not earlier), so that you don't leak it.
This all assumes that you are not using ARC. If you are using Xcode 4.2 or later, and can require iOS 4 or later, you should migrate your project to ARC, as it makes so many things much simpler. You would not need to retain or copy this object if you were using ARC, which means that migrating to ARC now is a fourth solution (but certainly a larger-scale one).
Objective-c how to convert NSURL into NSString?
Try This :
NSString *aStrUrl = [aUrlObj absoluteString];
How to convert NSURL to String in Swift
It turns out there are properties of NSURL you can access (see Swift Reference):
var directoryURL: NSURL
var urlString: String = directoryURL.absoluteString
// OR
var urlString: String = directoryURL.relativeString
// OR
var urlString: String = directoryURL.relativePath
// OR
var urlString: String = directoryURL.path
// etc.
NSString to NSURL?
This is how you create an NSURL
from an NSString
:
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
Xcode: NSString to NSUrl conversion issue
Thanks guys for your answers. I finally have fixed the issue and the issue was in the URL string I was passing as a parameter. It had some extra "" at the ends which were not showing up in the NSLog as it was a normal string, but when I removed those "" from the url string and then converted it to NSURL, it worked fine.
Convert NSURL from String
NSString *n = [NSString stringWithFormat:@"%@",@"http://somedomain.com/api/x?q={\"order_by\":[{\"field\":\"t\",\"direction\":\"desc\"}]}"];
NSURL *url = [NSURL URLWithString:[n stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"%@",url);
objective c convert special URL to NSURL
Please check below code, it is working,
NSString * mURL = @"http://cl3.webterren.com/1.gif?z=7&a=157ffb99379&b=%u5149%u660E%u7F51_%u65B0%u95FB%u89C6%u91CE%u3001%u6587%u5316%u89C6%u89D2%u3001%u601D%u60F3%u6DF1%u5EA6%u3001%u7406%u8BBA%u9AD8%u5EA6&B=UTF-8&c=http%3A//gmw.cn/%3F_wdxid%3D000000000000000000000000000000000000000000%26_wdc%3D0%26_wdt%3D012%26&d=&e=0&f=0&H=gmw.cn&E=0&r=6c4a9d9057c8bcef&s=0&t=0&u=1&i=zh-tw&j=0&k=320x568&l=32&m=&n=&o=8";
NSString * webStringURL = [mURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:webStringURL];
NSLog(@"uri host = %@", url.host);
NSLog(@"uri path = %@", url.path);
NSURL to NSString Conversion doing strange things
You are loading the contents of the file at the URL into the string. You are loading the file as an ASCII string (which that file almost certainly is not -- it's an image) and you are ignoring any errors.
To get the actual path for the url, you send the url the -path
message. So, to get a string with the path:
NSString* filePath = [theDoc path];
NSLog(@"the url: %@", theDoc);
NSLog(@"the path: %@", filePath);
This should print the following (based on your logs above):
the url: file://localhost/Users/*****/Desktop/mockupsite.jpg
the file: /Users/*****/Desktop/mockupsite.jpg
Related Topics
Swiftui - Animations Triggered Inside a View That's in a List Doesn't Animate the List as Well
How to Convert an Int to a Character in Swift
Swiftui Generic Pull to Refresh View
How to Change the Color of an Uiimage
How to Mimic Keyboard Animation on iOS 7 to Add "Done" Button to Numeric Keyboard
How to Create Static Library and Can Add Just .A File on Any Project in iOS
iOS 11 Large-Title Navigation Bar Not Collapsing
How to Catch All iOS Push Notifications with Different User Actions Including Tap on App Icon
Uiprogressview Custom Track and Progress Images in iOS 7.1
Swift - Compressing Video Files
What Impact Does Simulated Metrics Have
Application Not Automatically Launching in Device and Getting Error on Xcode
Google Maps iOS Sdk, Getting Directions Between 2 Locations
Installing Ffmpeg iOS Libraries Armv7, Armv7S, I386 and Universal on MAC with 10.8