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
rm...@latencyzero.com



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to