Questions tagged [anyobject]

`AnyObject` can be used to represent any type in Swift that is a class. Value types (Structs and enums) can be represented using `Any`. Use this tag for questions concerning the use of `AnyObject` and how to handle it.

AnyObject can be used to represent any type in Swift that is a class. Value types (Structs and enums) can be represented using Any. In migration from Objective-C to Swift, some methods might return a reference type in Objective-C whose type cannot be precisely known. This is where it's type is represented as AnyObject in Swift.

121 questions
4
votes
3 answers

Optional AnyObject values in Swift dictionary

When I add a value of type 'AnyObject?' to a dictionary of Type '[String : AnyObject]' in the following way, the value can not by added, which is actually what I've expected. Assigning an optional type to a non-optional type should fail in my…
4
votes
3 answers

Swift type inference and type checking issue

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 =…
Breek
  • 1,431
  • 13
  • 24
4
votes
2 answers

When should I use anyObject insted of UIButton in swift?

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.
3
votes
2 answers

Strange Any? as AnyObject behaviour

I'm struggling to understand the behaviour of the following code: let a: Any? = nil let b: AnyObject? = a as AnyObject if let c: AnyObject = b { print(c) print("That's not right, is it?") } else { print("I'd expect this to be…
Jan Nash
  • 1,883
  • 19
  • 30
3
votes
1 answer

Swift: AnyObject cast to Float failed

let json = [ "left" : 18, "deadline" : "May 10", "progress" : 0.6 ] as [String: AnyObject] let ss = json["progress"] as? Float let sss = json["progress"] as? Double print("ss = \(ss)\n sss = \(sss)") I have no idea why the ss shows…
Leaf
  • 203
  • 1
  • 2
  • 13
3
votes
1 answer

My App crashes when I pass a UITableViewCell as AnyObject? to an other function

I already know the solution for my problem, but I just really don't understand what's going on here. I have a UITableViewController that takes the cell in didSelectRow and uses it for something in an other function. I pass the cell as AnyObject?.…
Georg
  • 3,664
  • 3
  • 34
  • 75
3
votes
2 answers

Swift AnyObject Conversion

I created some test code to show the problem I am having. This compiles fine in a playground, however, when I try to put it into a project, Xcode gives the following warning: Treating a forced downcast to 'String' as optional will never produce…
Coder-256
  • 5,212
  • 2
  • 23
  • 51
3
votes
2 answers

Swift AnyObject - Down casting an array of protocols to [AnyObject]

How do you downcast an array of protocol instances into AnyObjects? I've tried some of the more reasonable ideas in the code example below. protocol Nameable : class { var name: String { get } } class Person: Nameable { var name: String …
edelaney05
  • 6,822
  • 6
  • 41
  • 65
3
votes
3 answers

How to convert [CustomClass] to [AnyObject]

I have an array of custom class [CustomClass]. I am trying to convert it to [AnyObject]. self.customClassArray = self.anyObjectArray as [AnyObject] I get the following errors: 'AnyObject' is not a subtype of 'CustomClass' and Cannot convert…
iOS
  • 3,526
  • 3
  • 37
  • 82
3
votes
2 answers

Cannot convert value of type 'AnyObject?' to expected argument type '[AnyObject]!

I'm using the swift lib "Sync" from Hyperoslo to convert a JSON user to a Core Data object. let user = JSON.valueForKey("user") Sync.changes(user , inEntityNamed: "User", dataStack: DataManager.manager, completion: { (response ) -> Void in …
Damien Romito
  • 9,801
  • 13
  • 66
  • 84
2
votes
2 answers

Why does SomeStruct() is AnyObject return true?

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…
Alex Larson
  • 137
  • 1
  • 7
2
votes
1 answer

Why casting function type as AnyObject works

Question : Difference between Any vs. AnyObject Answer : Any can represent an instance of any type at all, including function types and optional types. AnyObject can represent an instance of any class type. I tried to store a function type in a Any…
iOSDev
  • 326
  • 2
  • 10
2
votes
2 answers

How can this [AnyObject] return as AnyObject?

import Cocoa class Brain{ var internalProgram = [AnyObject]() var program:AnyObject{ get{ return (internalProgram as AnyObject) } } } var savedProgram: AnyObject? let brain = Brain() func save(){ …
Qasa iNto
  • 31
  • 3
2
votes
1 answer

Why `var one: AnyObject = [AnyObject]()` is valid?

I'm struggling to understand why an instance of AnyObject is equal to an array of Anyobject, i.e. Why this statement var one: AnyObject = [AnyObject]() is valid?
Thor
  • 9,638
  • 15
  • 62
  • 137
2
votes
1 answer

Swift as overload

I am creating simple Json Parser that works like that: I have JsonData class that contains Anyobject as data. When I use jsonData["key"] it returns JsonData to i can chain jsonData["key"]["key2"] etc. My question is how can I implement that class so…
Jarosław Krajewski
  • 520
  • 1
  • 7
  • 18
1
2
3
8 9