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
2
votes
1 answer

Appending Multidimensional Dictionary

I have a multidimensional dictionary and I am trying to add data into it without deleting data, but overwrite if duplicate. var items = [Int: AnyObject]() var IdsAndDetails = [String:AnyObject]() let index = 3 // static for test ... for result in…
senty
  • 12,385
  • 28
  • 130
  • 260
2
votes
2 answers

How to downcast an object that can be of multiple types?

Apple gives us an example of downcasting an object of the same type as such: let someObjects: [AnyObject] = [ Movie(name: "2001: A Space Odyssey", director: "Stanley Kubrick"), Movie(name: "Moon", director: "Duncan Jones"), …
mir-aclewhip
  • 127
  • 2
  • 11
2
votes
1 answer

Extending Optional>

I would like to make an extension on Optional>. How can I write this? I was expecting it to be something like extension Optional where Wrapped : Dictionary, Key : String, Value : AnyObject { ... } but it wants…
niklassaers
  • 8,480
  • 20
  • 99
  • 146
1
vote
0 answers

Swift declaring array of multiple optional type to Any not work as expected

I used Any type to store array of multiple optional type with default value, and got unexpected result. let i64: Int64? = nil print([ i64 ?? 0, i64 ?? 0, i64 ?? 0 ]) Then, [Optional(0), 0, 0] is printed. Following is my detailed…
1
vote
1 answer

Looking for a fix for Swift "ambiguous use of 'object(forKey:)'"

I've inherited some Swift code that I'm trying to compile. The following two lines generate an "Ambiguous use of object(forKey:)" error. let selectorString: String = (req as AnyObject).object(forKey: "selectorString") as! String let args = (req as…
David Nedrow
  • 1,098
  • 1
  • 9
  • 26
1
vote
0 answers

Why AnyObject or Any conversion is failing to GADUnifiedNativeAd in swift5?

I am using Google Ad and I am converting GADUnifiedNativeAd to AnyObject and converting back original GADUnifiedNativeAd is failing in Swift 5 . What can be the reason? Before this code working fine in Swift 4.2 and below: GADUnifiedNativeAd on…
TheCodeTalker
  • 685
  • 6
  • 14
1
vote
1 answer

When is a variable a AnyObject but not a NSObject

Imagine a variable declared as below: let sample : AnyObject = "anyobject" as AnyObject Now when I try to retrieve the type of the var sample, it shows as NSObject. Following is the code snippet: let sample : AnyObject = "anyobject" as…
Asha
  • 41
  • 5
1
vote
1 answer

swift - '(AnyObject)' is not a subtype of 'NSObject'

I'm new to swift development, I followed a tutorial and everything was fine until I came across this error and I don't know how to fix it. Can you help me ? if let json = response.result.value { let jsonArray:NSArray = json as! NSArray ///…
Obosso Tv
  • 13
  • 4
1
vote
1 answer

Swift: how to reserveCapacity() for SKSpriteNodes

I am trying to allocate sprite data to a global array using preset indexes for the sprites. I am initializing the array as an array of SKSpriteNodes. I am sending SKSpriteNodes to this array, each sprite has a set index for this array. I realize I…
WhatsGoood
  • 17
  • 4
1
vote
1 answer

How to filter objects of certain type from [AnyObject] array

Is it possible to filter an array of [AnyObject] to yield all elements of a given type, and none other? I can do it if the type is known at compile time: class MyClass1: CustomStringConvertible { var value: Int var description: String { …
1
vote
2 answers

How to Compare AnyObject Is or not?

I am writing below function to return value based on data objet that I passed to this method. If we get data null I am trying to return "-" otherwise If I get any valid response I am returning value.. private func checkDataForValue(data:…
tp2376
  • 690
  • 1
  • 7
  • 23
1
vote
1 answer

Why if I typecast String? to Any, Xcode gives off warning, but not with AnyObject?

This code gives warning "Expression implicitly coerced from 'String?' to Any." let email : String?; let password : String?; let dict = ["email": email, "password": password] as [String: Any]; But this code does not. let email : String?; let…
Chen Li Yong
  • 5,459
  • 8
  • 58
  • 124
1
vote
0 answers

How to save nested data model in Firebase? Swift Error: Can only store objects of > type NSNumber, NSString, NSDictionary, and NSArray

I get the error listed below when I try to save a nested model in Firebase. It is because the data model is not properly formatted, but I have searched for hours and I am stuck now. Cannot store object of type _SwiftValue at . Can only store…
bibscy
  • 2,598
  • 4
  • 34
  • 82
1
vote
3 answers

could not cast value of type 'Swift._ContiguousArrayStorage' to 'NSMutableArray'

I accidently updated iOS version in my phone from 10.3.3 to 11.0.3. Because of that I had to upgrade my xcode to latest 9.1 version. Now when I run my code in xcode 9.1 it appears could not cast value of type 'Swift._ContiguousArrayStorage' to…
Amila Prasad
  • 193
  • 1
  • 8
1
vote
0 answers

In SwiftyJSON, what is the difference between array, arrayValue and arrayObject?

I'm trying to better understand where would each have a distinct use case. From what I understand: array: is a simple optional array. arrayValue: is a non-optional array that will return an empty array if it's nil But I can't correctly understand…
mfaani
  • 33,269
  • 19
  • 164
  • 293
1 2
3
8 9