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

Google Mobile Native Express ads iOS - Issues with [AnyObject]

I'm trying to implement Native Express Ads in my iOS Project, with swift. First of all I have a class class Events { var _eventName: String! var _realDate: Date! var realDate: Date { if _realDate == nil { _realDate =…
Konstantinos Natsios
  • 2,874
  • 9
  • 39
  • 74
1
vote
1 answer

Filter array of Any based on variable

I have an array of type Any in that array there are two different types, Person and SportsMan both these has a value called rank. I want to sort my array based on the rank. Here is how I have done it today and it works: self.persons.sort { let a…
Aker Abos
  • 21
  • 4
1
vote
1 answer

why do i keep getting “AnyObject cannot be constructed because it has no accessible initializers”

I have been following a tutorial on youtube and been coding a twitter like app in xcode and keep returning this error, the tutorial was using an earlier version of swift. How do i move past this? class HandleViewController: UIViewController { …
jon
  • 19
  • 1
1
vote
0 answers

Subscript operator with AnyObject in Swift

I have observed that it is possible to use the subscript operator with AnyObject instances in Swift. For example: let x: AnyObject = NSDictionary() print(type(of: x["y"])) This code produces the following…
Greg Brown
  • 3,168
  • 1
  • 27
  • 37
1
vote
1 answer

Is it correct to say all erased types are non-initializable?

(By erased types I mean: (Any, AnyHashable, AnyObject), protocol, or root class NSObject). I'm asking this question because in the code below I can't initialize something to AnyObject. I get error: cannot invoke initializer for type 'AnyObject?'…
mfaani
  • 33,269
  • 19
  • 164
  • 293
1
vote
0 answers

Swift - structures and AnyObject

I thought that structs were not AnyObject. For instance: https://stackoverflow.com/a/33921992/2054629 But then I tried (in swift 3, xcode 8.2.1): struct Struct{ let foo = 1 } let s = Struct() s is AnyObject // true, and I get a warning saying…
Guig
  • 9,891
  • 7
  • 64
  • 126
1
vote
2 answers

Combining UnitMass and UnitLength arrays

I'm trying to combine two measurement arrays: var unitMasses: [UnitMass] { return [.milligrams, .grams, .kilograms, .ounces, .pounds] } var unitLengths: [UnitLength] { return [.centimeters, .decimeters, .meters] } into one: var units:…
ajrlewis
  • 2,968
  • 3
  • 33
  • 67
1
vote
1 answer

Array storing Any objects is also of AnyObject type

let f: () -> Void = { } let array = ["a", 1, false, f] as [Any] if array[3] is AnyObject { print(array[3]) } Why is the element evaluating to true for AnyObject even though the array is set to store Any? Why is function evaluates to true as an…
Boon
  • 40,656
  • 60
  • 209
  • 315
1
vote
3 answers

IOS swift how can i check for empty array while getting Json data

I am very new to swift and in my code I get JSON from a certain email, the JSON data is first put into an Array in swift, however if the array is null or there is no Json Data I get this error Could not cast value of type 'NSNull' (0x1067a5748) to…
user1949387
  • 1,245
  • 3
  • 21
  • 38
1
vote
2 answers

How do I correctly check for a successful downcast to AnyObject?

I'm working with something trivialized to this: func isAnyObject(someObject: T) { if let object = someObject as? AnyObject { // do something } else { // do something else } } Which gives me the compiler warnings:…
Steals
  • 117
  • 1
  • 8
1
vote
0 answers

Why aren't Swift Strings bridged to NSString in a Dictionary when Foundation is imported?

According to this answer on the question Value of type 'String' does not conform to expected dictionary value type 'AnyObject', importing Foundation should implicitly bridge a Swift String struct to NSString, so it can be used in [String :…
JAL
  • 41,701
  • 23
  • 172
  • 300
1
vote
1 answer

Syntax changes in Swift 3 with Xcode 8

Recently, I convert to swift 3 and many errors were occurring. Now I convert each lines of codes to swift 3. :( I would like to know how to write loop in swift 3? Here it is ... for index in 0...((data as AnyObject).count)!-1{ …
May Phyu
  • 895
  • 3
  • 23
  • 47
1
vote
1 answer

Swift3 error : Type 'NSFastEnumerationIterator.Element' (aka 'Any') does not conform to protocol 'AnyObject'

So, I'm trying to update my toDo list app to swift 3 / iOS 10, but I came across this error when updating. I have looked up the internet and StackOverflow and found that someone else also sort of had this problem, but the solution there, didn't help…
Jenoah Kers
  • 157
  • 2
  • 2
  • 7
1
vote
0 answers

Cast from AnyObject to UIEdgeInsets

I'm trying to use KVO pattern on scrollView's contentInset and i need to know about old/new values. I know UIEdgeInests is a struct hence can not be stored in Dictionary, but i receive it in changes. this is my change Dictionary: Optional(["old":…
farzadshbfn
  • 2,710
  • 1
  • 18
  • 38
1
vote
3 answers

Casting AnyObject to Specific Class

I'm using socket.io Swift Library. With the following line of code, socket.on("info") { (dataArray, socketAck) -> Void in let user = dataArray[0] as? User print(user._id) } dataArray[0] is a valid object but user appears to…
Serhan Oztekin
  • 475
  • 7
  • 20
1 2 3
8 9