Swift: how to declare a property with class names

For example, this snippet is taken from the definition UIColorin Swift:

// Access the underlying CGColor or CIColor.
public var CGColor: CGColor { get }

However, trying to do the same, say NSError, in my own class causes an error:

public class MyClass {
    public var NSError: NSError {
        return NSError(domain: "mydomain", code: 0, userInfo: [:])
    }
}

The line varshows Use of undeclared type 'NSError'.

Is it possible? What a good way to do this?

<sub> Apple Swift version 2.2 (swiftlang-703.0.18.1 clang-703.0.29)
Target: x86_64-apple-macosx10.9 sub>

+4
source share
1 answer

You can do this by creating typealiasfor NSError, NSErrorRef. You will then refer to these message types in your class, as NSErrorit will now refer to a property, not a class.

public typealias NSErrorRef = NSError

public class MyClass {
    public var NSError:NSErrorRef {
        return NSErrorRef(domain: "", code: 0, userInfo: nil)
    }
}

.

, , CGColor , Swift UIColor Objective-C. , - , . , UIColor, CGColor, CGColor ( Swift), , Apple , !

, ( ). :

public class MyClass {
    public var nsError:NSError {
        return NSError(domain: "", code: 0, userInfo: nil)
    }
}

, , error - Swift ( ), ).

public class MyClass {
    public var error:NSError {
        return NSError(domain: "", code: 0, userInfo: nil)
    }
}
+4

All Articles