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
1
vote
2 answers

Ios Swift decode json data sent from GCM notification

I am receiving the GCM Json encoded data as AnyObject as below [MsgKey: {"NamKey":"Bdy","MobKey":"9964120147","UidKey":"Uid31"}, collapse_key: do_not_collapse, from: 925652137353] I want to decode the above and pass it to local notication screen I…
Sujay U N
  • 4,974
  • 11
  • 52
  • 88
1
vote
1 answer

Type Casting For AnyObject to represent instances of any reference type

var array = [AnyObject]() struct Test {} array.append(Test()) When I write this code in the play ground it gives me the following error Type 'Test' does not conform to protocol 'AnyObject' I am guessing it is failing because struct is a value…
user4410715
1
vote
3 answers

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

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…
Jim
  • 1,260
  • 15
  • 37
1
vote
1 answer

Could not cast value of type 'Swift.Array<(Swift.String, Swift.String)>' to 'Swift.AnyObject'

My swift code look like below Family.arrayTuple:[(String,String)]? = [] Family.arrayTupleStorage:String? Family.arrayTupleStorage:String = (newDir! as…
WoderMan
  • 53
  • 1
  • 12
1
vote
2 answers

Populating prepareForSegue's destinationViewController via returned type

Good afternoon! I'm relatively new to Swift, though I think I've managed to wrap my head around most of it, however I'm having difficulties setting a segue's destinationViewController indirectly. I understand that destinationViewController accepts…
0
votes
0 answers

Retrieve value from AnyObject to check against a particular constraint in Swift

I have an object that is returned as an AnyObject from a 3rd party SDK, which I have no control over. The stream gives me a collection of bluetooth devices, and comes in the flavour of: AnyObject object [__NSDictionaryM] [0] = "dataElement" :…
JadedEric
  • 1,943
  • 2
  • 26
  • 49
0
votes
2 answers

Testing type for class conformance in Swift

EDIT: The previous answers alluded to in the comments don't answer the question, which was how to determine if any given Type was a reference type and how to safely conform said type to AnyObject. Testing against the passed type doesn't work, as the…
Michael Long
  • 1,046
  • 8
  • 15
0
votes
1 answer

how do I make a swiftui viewmodifier that accepts any object?

I have a quite few reusable viewmodifiers in my projects, but I have never been able to make one that accepts any object instead of a specific object. Fx. in below viewmodifier, how would I make it accept any object instead of just "StopContent" so…
lmunck
  • 505
  • 4
  • 12
0
votes
1 answer

Why I am getting 'valueForUndefinedKey' error when using AnyObject in Swift?

Im have a question. Why the following crash reason? since "name" exists? viewcontroller1: struct pointDict { let id: Int let name: String } var pointsPlane: [pointDict] = [] let wp = pointDict(id: 123, name:…
0
votes
2 answers

From anyObject to array of coordinates

New to Swift and json I want to extract lng,lat from anyObject elements. Here a part of my code to extract from json file: for (key,value) in geoFeature! as [String: AnyObject] {print("key=",key, ptvir, value)} let geoCoords =…
0
votes
3 answers

Swift 5: How to test an AnyObject? variable for it's value's type?

I'm currently porting an iOS app from Objective-C to Swift. While doing that I just stumbled across a problem I'm struggling with. I have a variable ("value") which can bet set to AnyObject?, especially can it be of type String, Array, Dictionary…
stm
  • 121
  • 1
  • 6
0
votes
1 answer

Swift: Confused about AnyObject declaration

I know AnyObject can be any class type, but what I'm confused about is the declaration of AnyObject, I have read a book and some articles saying it's a protocol, but when I looked at AnyObject declaration it shows me that it is a typealias AnyObject…
Zyz
  • 123
  • 1
  • 9
0
votes
1 answer

Best way to compare two any type values in swift

I have a class which has property value of type Any I have implemented a didSet method for the value like this didSet { if oldValue == nil && self.value != nil { // do something } else { if let val = self.value as? AnyHashable, let…
Avinash Sharma
  • 665
  • 1
  • 7
  • 23
0
votes
2 answers

How can you create an infix operator that applies to every class and struct?

I've created a 'configure' infix operator '=>' which lets me configure objects inline, allowing me to both define and initialize properties and/or call methods/functions at the assignment level. For instance, instead of this, which requires an…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
0
votes
2 answers

Can't perform methods of objects stored in Array[Any]

I want to store objects of different types in an array. The program below is only a minimum demo. In the anyArray:[Any] an instance of Object1 is stored. The print statement prints out the expected object type. In the following line the test of the…
Heinz M.
  • 668
  • 3
  • 8
1 2 3
8 9