More Swift issues, NSURLComponents
More Swift issues, NSURLComponents
- Subject: More Swift issues, NSURLComponents
- From: Rick Mann <email@hidden>
- Date: Mon, 05 Oct 2015 18:03:10 -0700
If I import UIKit in here, I get seemingly ridiculous errors:
=============================
import Foundation
import UIKit
if let url = NSURLComponents(string: "http://apple.com/foo/bar?q=123")
{
let url2 = url
url2.path = nil
let url3 = url.copy()
url3.path = nil
}
playground19.swift:10:2: error: ambiguous use of 'path'
url3.path = nil
^
QuartzCore.CAKeyframeAnimation:4:13: note: found this candidate
@objc var path: CGPath? { get set }
^
Foundation.NSURL:50:13: note: found this candidate
@objc var path: String? { get }
^
=============================
Oh, right, it's AnyObject. But why doesn't it find the path on NSURLComponents?
Remove the UIKit import, and it's better:
=============================
import Foundation
if let url = NSURLComponents(string: "http://apple.com/foo/bar?q=123")
{
let url2 = url
url2.path = nil
let url3 = url.copy()
url3.path = nil
}
playground20.swift:9:12: error: cannot assign to property: 'url3' is a 'let' constant
url3.path = nil
~~~~ ^
/var/folders/16/jfb809_s2fz01ql7k494f6pc0000gn/T/./lldb/33089/playground20.swift:8:2: note: change 'let' to 'var' to make it mutable
let url3 = url.copy()
^~~
var
=============================
But why does it allow the url2.path assignment? Both of those are "let".
Anyway, let's follow the compiler's advice:
=============================
import Foundation
if let url = NSURLComponents(string: "http://apple.com/foo/bar?q=123")
{
let url2 = url
url2.path = nil
var url3 = url.copy()
url3.path = nil
}
playground21.swift:9:12: error: cannot assign to property: 'url3' is immutable
url3.path = nil
~~~~ ^
=============================
Nope, still no good.
Let's cast things better:
=============================
import Foundation
if let url = NSURLComponents(string: "http://apple.com/foo/bar?q=123")
{
let url2 = url
url2.path = nil
let url3 = url.copy() as! NSURLComponents
url3.path = nil
}
=============================
No errors! But url3 is "let." Why is it allowed here?
The final question: Is there a type-safe mutable copy in Swift that's not so verbose as copy-with-cast?
--
Rick Mann
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden