anyobject

Why does SomeStruct() is AnyObject return true? [duplicate]

社会主义新天地 提交于 2021-02-04 19:41:07
问题 This question already has an answer here : Why is casting a struct to AnyObject not a compile error in swift? (1 answer) Closed last year . I'm a bit confused by the usage of AnyObject. Let me provide a few examples. AnyObject NSObject() is AnyObject ^ true (as expected) class MyClass {} MyClass() is AnyObject ^ true (as expected) class MyClass {} MyClass.self is AnyObject ^ true (as expected) String() is AnyObject ^ true (as NOT expected) struct MyStruct {} MyStruct() is AnyObject ^ true (as

swift类型转换is和as

旧时模样 提交于 2020-05-02 10:52:09
Swift中类型转换使用 is 和 as 操作符。使用这两个操作符可以检查值的类型或者转换他的类型 一、类型检查 class Animal: NSObject { var name: String! init(name: String) { self.name = name } } class Dog: Animal { var color: UIColor! init(name: String,color: UIColor) { self.color = color super.init(name: name) } } class Cat: Animal { var weight: Float! init(name: String,weight: Float) { self.weight = weight super.init(name: name) } } let animals = [Dog.init(name: "tiger", color: UIColor.yellow), Dog.init(name: "wangcai", color: UIColor.black), Cat.init(name: "hellokitty", weight: 10.0)] 如上创建三个类: Animal,Dog,Cat ,其中Cat和Dog继承与Animal 。创建了一个容器

Swift Generics vs Any

♀尐吖头ヾ 提交于 2020-01-21 07:31:45
问题 I read swift documentation in apple site. There is a function swapTwoValues, which swaps two any given values func swapTwoValues1<T>(_ a: inout T, _ b: inout T) { let temporaryA = a a = b b = temporaryA } Now I want to write similar function but instead of using T generic type I want to use Any func swapTwoValues2(_ a: inout Any, _ b: inout Any) { let temporaryA = a a = b b = temporaryA } to call this functions I write var a = 5 var b = 9 swapTwoValues1(&a, &b) swapTwoValues2(&a, &b) I have

How can this [AnyObject] return as AnyObject?

痞子三分冷 提交于 2019-12-22 18:45: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?

When should I use anyObject insted of UIButton in swift?

ぃ、小莉子 提交于 2019-12-22 10:25:20
问题 When should I use anyObject insted of UIButton in swift? I am making an IBAction for my button that will be used to do more than on task on of the tasks is to switch to the next view. 回答1: Ultimately, it really doesn't matter. You can choose to use the parameter of (sender: AnyObject) or you can use (sender: UIButton) . There might be times however where you might have to cast AnyObject as a UIButton if you need access to the properties provided by UIButton . For example let's say you have

sort array of anyobject Swift 3

久未见 提交于 2019-12-20 06:48:13
问题 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

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

痴心易碎 提交于 2019-12-20 06:07:11
问题 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

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

旧城冷巷雨未停 提交于 2019-12-20 06:07:10
问题 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

How do I convert a UserDefault value from AnyObject? to an Int so I can save it

烈酒焚心 提交于 2019-12-13 19:55:18
问题 I'm trying to learn how to work with UserDefaults and can't figure out how to store one of the values I need to store. I have this enum: enum CalculationFormula: Int { case Epley case Brzychi case Lander case Lombardi case MayhewEtAl case OConnerEtAl } and this class with a property called 'selectedFormula' that I want to be able to store as a NSUserDefaults value: class CalculatorBrain: NSObject { var weightLifted: Double var repetitions: Double var oneRepMax: Double? var selectedFormula =

Non-optional expression of type 'AnyObject' used in a check for optionals

我的梦境 提交于 2019-12-12 10:35:38
问题 I created an extension on 'Dictionary' to help me parse JSON. The method below helps me do this: func toJSONString() -> String? { if let dict = self as? AnyObject { if let data = try? JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions(rawValue: 0)) { if let json = String(data: data, encoding: String.Encoding.utf8) { return json } } } return nil } The issue occurs on this line: if let dict = self as? AnyObject { I get a warning saying "Non-optional