An object that parses URLs into and constructs URLs from their constituent parts that bridges to URLComponents
; use NSURLComponents
when you need reference semantics or other Foundation-specific behavior.
SDKs
- iOS 7.0+
- macOS 10.9+
- tvOS 9.0+
- watchOS 2.0+
Framework
- Foundation
Overview
The NSURLComponents
class is a class that is designed to parse URLs based on RFC 3986 and to construct URLs from their constituent parts. Its behavior differs subtly from the NSURL
class, which conforms to older RFCs. However, you can easily obtain an NSURL
object based on the contents of a URL components object or vice versa.
You create a URL components object in one of three ways: from an NSString
object that contains a URL, from an NSURL
object, or from scratch by using the default initializer. From there, you can modify the URL’s individual components and subcomponents by modifying various properties, either in unencoded form or in URL-encoded form. If you set the unencoded property, you can then obtain the encoded equivalent by reading the encoded property value and vice versa.
Important
The Swift overlay to the Foundation framework provides the URLComponents
structure, which bridges to the NSURLComponents
class. For more information about value types, see Working with Cocoa Frameworks in Using Swift with Cocoa and Objective-C (Swift 4.1).