---
title: "componentsWithString:encodingInvalidCharacters:"
framework: foundation
role: symbol
role_heading: Type Method
path: "foundation/nsurlcomponents/componentswithstring:encodinginvalidcharacters:"
---

# componentsWithString:encodingInvalidCharacters:

Returns a URL components instance from the provided string, optionally IDNA- and percent-encoding any invalid characters.

## Declaration

```occ
+ (instancetype) componentsWithString:(NSString *) URLString encodingInvalidCharacters:(BOOL) encodingInvalidCharacters;
```

## Parameters

- `URLString`: The URL string to parse.
- `encodingInvalidCharacters`: A Boolean value that indicates whether the initializer attempts to encode any invalid characters in URLString.

## Return Value

Return Value A URL components instance from the provided string, optionally with invalid characters percent-encoded.

## Discussion

Discussion If encodingInvalidCharacters is true, this initializer tries to encode the string to create a valid URL. If the URL string is still invalid after encoding, the method returns nil.

## See Also

### Creating URL components

- [componentsWithString:](foundation/nsurlcomponents/componentswithstring:.md)
- [componentsWithURL:resolvingAgainstBaseURL:](foundation/nsurlcomponents/componentswithurl:resolvingagainstbaseurl:.md)
- [init()](foundation/nsurlcomponents/init().md)
- [init(string:)](foundation/nsurlcomponents/init(string:).md)
- [init(string:encodingInvalidCharacters:)](foundation/nsurlcomponents/init(string:encodinginvalidcharacters:).md)
- [init(url:resolvingAgainstBaseURL:)](foundation/nsurlcomponents/init(url:resolvingagainstbaseurl:)-3bbte.md)
