How to convert Array of string to Array of URL? - swift

Need to convert array of string value to array of URL. Some one help me to convert the String array to URL array in swift.

let urls = ["www.1.com", "www.1.com", "www.1.com", "randomSmth"].compactMap { URL(string: $0) }
"compactMap" ensures that it just ignores invalid urls.

Use compact map to filter non valid URLs
let arrayOfStrings = ["https://www.google.com","https://www.facebook.com", "notRerallyAnURL"]
let arrayOfURLs = arrayOfStrings.compactMap { URL(string:$0) }

Related

Filter An Array Of Arrays By A Value In Firestore Swift

I'm looking to filter an array of arrays by specific value of one of the keys located within each array. Each nested array is read in from Firestore.
As an object, each nested array would look like this:
struct Video {
var url: String
var submissionDate: Timestamp
var submittingUser: String
}
I'm reading it in like this:
videos = document.get("videos") as? [[String : Any]] ?? nil
So far so good, but when I filter it like this:
filteredVideos = videos.filter { $0[2].contains(self.userIdentification) }
I can't do it without getting the error "Reference to member 'contains' cannot be resolved without a contextual type," an error which I was unable to find any relevant information on SO about.
I have read that some people say "Don't use arrays in Firestore!" but this is a build requirement.
Anyone have any ideas? Basically just need all arrays within the array where userId == submittingUser.
Reference Article:
I tried the answer from here: How to filter out a Array of Array's but no luck for this situation.
It's actually an array of dictionaries, not an array of arrays. All you need to do is construct the right predicate for the filter.
This is basically what your Firestore data looks like:
let videos: [[String: Any]] = [
["url": "http://www...", "submittingUser": "user1"],
["url": "http://www...", "submittingUser": "user2"]
]
This is the user you're looking for:
let userIdentification = "user2"
This is the predicate for the filer:
let filteredVideos = videos.filter { (video) -> Bool in
if let submittingUser = video["submittingUser"] as? String,
submittingUser == userIdentification {
return true
} else {
return false
}
}
You can shorthand this down to a single line if you're okay with force-unwrapping the dictionary (if you're 100% certain every video will have a valid submittingUser value):
let filteredVideos = videos.filter({ $0["submittingUser"] as! String == userIdentification })

Array (Inside of a String) Conversion, Swift 4

Does anyone know the simplest way to convert an Array inside of a String to an actual Array without effecting any characters?
For Example:
var array: [String] = []
let myStr = "[\"Hello,\", \"World\"]"
// I would like 'array' to store: ["Hello,", "World"]
I thought it could be done with Array(myStr) but that would only make an array of all the characters.
Help would be appreciated. Thank you.
You can decode it with JSONDecoder, since it is a JSON.
Example below:
let myStr = "[\"Hello,\", \"World\"]"
let data = Data(myStr.utf8)
do {
let decoded = try JSONDecoder().decode([String].self, from: data)
print("Decoded:", decoded)
} catch {
fatalError("Error")
}
// Prints: Decoded: ["Hello,", "World"]
What is happening:
Your myStr string is converted to Data.
This data is then decoded as [String] - which is an array of strings.
The decoded data of type [String] is then printed.
We use a do-try-catch pattern to catch errors, such as incorrect format or data entered.

Using String addingPercentEncoding to encode a string as query param

I have an input string "+20" and I am trying to pass that as query parameter in url.
So I am trying to encode the myInputString by doing
let s1 = myInputString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
But in the debugger, the string s1 still shows as '+20' instead of '%2B20'
Is there something I did wrong?
As already mentioned by matt + is a legal URL character. If you really need to encode it you would need to create your own custom urlQueryAllowed and subtract the plus sign from it:
extension CharacterSet {
static let allowedCharacters = urlQueryAllowed.subtracting(.init(charactersIn: "+"))
}
let myInputString = "+20"
let s1 = myInputString.addingPercentEncoding(withAllowedCharacters: .allowedCharacters) // "%2B20"

Cannot convert value of type Substring to expected argument type String - Swift 4

Trying to get substring of String and append it to array of Strings:
var stringToSplit = "TEST TEXT"
var s = [String]()
let subStr = anotherString[0 ..< 6]
s.append(subStr) // <---- HERE I GET THE ERROR
As #Leo Dabus mentioned, you need to initialize a new String with your substring:
Change:
s.append(subStr)
To:
s.append(String(subStr))
my two cents for serro in different context.
I was trying to get an array of "String" splitting a string.
"split" gives back "Substring", for efficiency reason (as per Swift.org litre).
So I ended up doing:
let buffer = "one,two,three"
let rows = buffer.split(separator:",")
let realStrings = rows.map { subString -> String in
return String(subString)
}
print(realStrings)
Ape can help someone else.

How to get the video ID of a YouTube string

(XCode 6.3.2, Swift 1.2)
After researching the internet for the whole evening I already know that this can't be done that easily.
I simply want to get the video ID of a YouTube link. So the "ABCDE" in "https://www.youtube.com/watch?v=ABCDE"
What I got so far:
var url = "https://www.youtube.com/watch?v=ABCDE"
let characterToFind: Character = "="
let characterIndex = find(url, characterToFind)
println(characterIndex)
url.substringFromIndex(advance(url.startIndex, characterIndex))
Prinln outputs: Optional(31)
That's right but I can't use this because it's no index.
XCode also states for the last line: Missing argument for parameter #3 in call
I also don't know what the 3rd parameter of substringFromIndex should be.
Many thanks in advance!
In your case there is no need to create an NSURL as other answers do. Just use:
var url = "https://www.youtube.com/watch?v=ABCDE"
if let videoID = url.componentsSeparatedByString("=").last {
print(videoID)
}
Swift 3+ version:
var url = "https://www.youtube.com/watch?v=ABCDE"
if let videoID = url.components(separatedBy: "=").last {
print(videoID)
}
You can use NSURL query property as follow:
Xcode 8.3.1 • Swift 3.1
let link = "https://www.youtube.com/watch?v=ABCDE"
if let videoID = URL(string: link)?.query?.components(separatedBy: "=").last {
print(videoID) // "ABCDE"
}
Another option is to use URLComponents:
let link = "https://www.youtube.com/watch?v=ABCDE"
if let videoID = URLComponents(string: link)?.queryItems?.filter({$0.name == "v"}).first?.value {
print(videoID) // "ABCDE"
}
Swift 1.x
let link = "https://www.youtube.com/watch?v=ABCDE"
if let videoID = NSURL(string: link)?.query?.componentsSeparatedByString("=").last {
println(videoID) // "ABCDE"
}
You need to unwrap the optional to use the index:
var url = "https://www.youtube.com/watch?v=ABCDE"
let characterToFind: Character = "="
if let index = find(url, characterToFind) { // Unwrap the optional
url.substringFromIndex(advance(index, 1)) // => "ABCDE"
}
find returns an optional – because the character might not be found, in which case it will be nil. You need to unwrap it to check it isn’t and to get the actual value out.
You can also use a range-based subscript on the index directly, rather than using advance to turn it into an integer index:
let url = "https://www.youtube.com/watch?v=ABCDE"
if let let characterIndex = find(url, "=") {
let value = url[characterIndex.successor()..<url.endIndex] // ABCDE
}
else {
// error handling, if you want it, here
}
You have more options if there is a reasonable default in the case of “not found” For example, if you just want an empty string in that case, this will work:
let idx = find(url, "=")?.successor() ?? url.endIndex
let value = url[idx..<url.endIndex]
Or, maybe you don’t even need to deal with the optionality right now, so you’re happy to leave the result optional as well:
// value will be Optional("ABCD")
let value = find(url, "=").map { url[$0.successor()..<url.endIndex] }
For a rundown on optionals, take a look here and here.
Also, rather than hand-parsing URL strings at all, you might find the info here useful.
With your url format, you can get the 'v' parameter's value by converting to NSURL then get parameters and separate it by '='.
var url: NSURL = NSURL(string: "https://www.youtube.com/watch?v=RAzOOfVA2_8")!
url.query?.componentsSeparatedByString("=").last
url.query returns v=RAzOOfVA2_8
If the link has more than 1 parameter, you can get all parameters then do a loop to verify each parameter:
var url: NSURL = NSURL(string: "https://www.youtube.com/watch?v=RAzOOfVA2_8")!
var params = url.query?.componentsSeparatedByString("&")
if let _params = params { //have parameters in URL
for param in _params { //verify each pair of key & value in your parameters
var _paramArray = param.componentsSeparatedByString("=") //separate by '=' to get key & value
if (_paramArray.first?.compare("v", options: nil, range: nil, locale: nil) == NSComparisonResult.OrderedSame) {
println(_paramArray.last)
}
}
} else {
//url does not have any parameter
}