anyobject

Swift type inference and type checking issue

依然范特西╮ 提交于 2019-12-10 17:22:12
问题 I'm not looking for an answer like how to do it correctly but why this happens. Here is the code: func isInt(param: AnyObject?) { if let value = param as? Int { print(value) } else { print("Not Int") } if let value = param { if value is Int { print(value) } else { print("Not Int") } } } let a:AnyObject? = 1.2 let b:Float? = 1.2 let c:Double? = 1.2 isInt(a) isInt(b) isInt(c) I understand in the first if loop, the param is casted to Int and then print out 1 . But why in second if loop, if value

Casting from AnyObject to CGColor? without errors or warnings

◇◆丶佛笑我妖孽 提交于 2019-12-08 19:34:17
问题 Hello StackOverflow :) I've been running into a weird problem since I upgraded to swift 2.0 I'm trying to set a border color, so I'm writing self.layer.borderColor = borderColor as! CGColor where borderColor is an AnyObject , while self.layer.borderColor is a CGColor? If I write self.layer.borderColor = borderColor as! CGColor I get the warning Treating a forced downcast to 'CGColor' as optional will never produce 'nil' and is recommended to instead use as? If I instead write self.layer

How can this [AnyObject] return as AnyObject?

ぐ巨炮叔叔 提交于 2019-12-06 13:42:12
import Cocoa class Brain{ var internalProgram = [AnyObject]() var program:AnyObject{ get{ return (internalProgram as AnyObject) } } } var savedProgram: AnyObject? let brain = Brain() func save(){ savedProgram = brain.program } How can this internalProgram:[AnyObject] return as AnyObject without Xcode giving a warning or an error? I know that program 's type is set as AnyObject already but I mean how can this work and wasn't it supposed to be [AnyObject] ? So why no any warning or error issue? Hamish So why no any warning or error issue? There would have been if you hadn't of said as AnyObject

Class-only generic constraints in Swift

人走茶凉 提交于 2019-12-04 08:18:15
问题 I'm trying to mark a variable of a generic type as weak: class X<T> { weak var t: T? } If I don't put in any constraints for T I get the error weak cannot be applied to non-class type 'T' . If I would only use use this with NSObject derived classes this would work: class X<T: NSObject> { weak var t: T? } But I also want to be able to use pure Swift classes. For protocols it is possible to require that the implementor is of class type by using the class keyword: protocol ClassType: class { }

Class-only generic constraints in Swift

与世无争的帅哥 提交于 2019-12-02 22:19:03
I'm trying to mark a variable of a generic type as weak: class X<T> { weak var t: T? } If I don't put in any constraints for T I get the error weak cannot be applied to non-class type 'T' . If I would only use use this with NSObject derived classes this would work: class X<T: NSObject> { weak var t: T? } But I also want to be able to use pure Swift classes. For protocols it is possible to require that the implementor is of class type by using the class keyword: protocol ClassType: class { } With the ClassType protocol I can now mark the variable as weak: class X<T: ClassType> { weak var t: T?

Cannot invoke initializer for type 'AnyObject?' with no arguments

不打扰是莪最后的温柔 提交于 2019-12-02 10:59:35
I am using swift 3 to build a type of chat messenger. To store loggedIn Users, I try to use an onject of type AnyObject. var loggedInUser = AnyObject?() And it gives the following error: ViewController.swift:29:24: Cannot invoke initializer for type 'AnyObject?' with no arguments 来源: https://stackoverflow.com/questions/39515792/cannot-invoke-initializer-for-type-anyobject-with-no-arguments

sort array of anyobject Swift 3

允我心安 提交于 2019-12-02 08:01:58
I am trying to sort an array of anyobject, but not able to do it.I get some data from Parse database in AnyObject format. As per below data, I want to sort this AnyObject array by "NAME". Below is my code - let sortedArray = (myArray as! [AnyObject]).sorted(by: { (dictOne, dictTwo) -> Bool in let d1 = dictOne["NAME"]! as AnyObject; // this line gives error "Ambiguous use of subscript" let d2 = dictTwo["NAME"]! as AnyObject; // this line gives error "Ambiguous use of subscript" return d1 < d2 }) myArray looks like this - { LINK = "www.xxx.com"; MENU = Role; "MENU_ID" = 1; NAME = "A Name";

How Does AnyObject Conform to NSObjectProtocol?

我的未来我决定 提交于 2019-11-30 19:35:41
This question was inspired by mz2's answer on the question Check for object type fails with "is not a type" error . Consider an empty Swift class: class MyClass { } Attempting to call any NSObjectProtocol methods on an instance of this class will result in a compile-time error: let obj = MyClass() obj.isKindOfClass(MyClass.self) // Error: Value of type 'MyClass' has no member 'isKindOfClass' However, if I cast the instance as AnyObject , my object now conforms to NSObjectProtocol and I can call the instance methods defined by the protocol: let obj: AnyObject = MyClass() obj.isKindOfClass

How Does AnyObject Conform to NSObjectProtocol?

自作多情 提交于 2019-11-30 03:48:59
问题 This question was inspired by mz2's answer on the question Check for object type fails with "is not a type" error. Consider an empty Swift class: class MyClass { } Attempting to call any NSObjectProtocol methods on an instance of this class will result in a compile-time error: let obj = MyClass() obj.isKindOfClass(MyClass.self) // Error: Value of type 'MyClass' has no member 'isKindOfClass' However, if I cast the instance as AnyObject , my object now conforms to NSObjectProtocol and I can

Swift专题讲解十九——类型转换

 ̄綄美尐妖づ 提交于 2019-11-29 16:51:13
Swift专题讲解十九——类型转换 一、类型检查与转换 在Objective-C和Java中,任何类型实例都可以通过强转使编译器认为它是另一种类型的实例,这么做其实是将所有的安全检查工作都交给了开发者自己来做。先比之下,Swift中的Optional类型转换就会比较安全与可靠。 Swift中使用is关键字来进行类型的检查,其会返回一个布尔值true或者false来表明检查是否成立,示例如下: var str = "HS" if str is String { print(str) } Swift中有向上兼容与向下转换的特性,就是说,一个父类类型的集合可以接收子类的实例,同样,在使用这些实例变量时可以将其向下转换为子类类型,示例如下: //自定义一个类及其子类 class MyClass { var name:String? } class MySubClassOne: MyClass { var count:Int? } class MySubClassTwo: MyClass { var isBiger:Bool? } //创建3个实例 var obj1 = MyClass() obj1.name = "HS" var obj2 = MySubClassOne() obj2.count = 100 var obj3 = MySubClassTwo() obj3.isBiger