Get a Unique String for a Given Anyobject

Get a unique String for a given AnyObject?

func hashString (obj: AnyObject) -> String {
return String(ObjectIdentifier(obj).uintValue)
}

let id = hashString(obj)

Swift 3.0

return String(UInt(ObjectIdentifier(obj))

Swift 4.1

return String(UInt(bitPattern: ObjectIdentifier(obj)))

Get a unique String for a given AnyObject?

func hashString (obj: AnyObject) -> String {
return String(ObjectIdentifier(obj).uintValue)
}

let id = hashString(obj)

Swift 3.0

return String(UInt(ObjectIdentifier(obj))

Swift 4.1

return String(UInt(bitPattern: ObjectIdentifier(obj)))

Get from AnyObject(NSString) to String

I ended up with the ugly line:

var uuidString:String = regionToMonitor["uuid"] as! String

no warnings, no errors, no runtime error

How to convert the [AnyObject] Array to string in swift

First map the values to String values, and then just join them using separator that you like:

let description: String = ["A12", 1, 2, "Test"].map{ String(describing: $0) }.joined(separator: ", ")
print(description)

Cannot subscript a value of type '[[String : AnyObject]]' with an index of type 'String'

You are casting the type as:

[[String: AnyObject]]

When you should be casting it like this:

[String: AnyObject]

The way you are doing it casts the type as an array of arrays that contain dictionaries when you should want to cast it as an array of dictionaries that contain a key as a strong and any object as a value.

Hope this helped :)

Get from AnyObject(NSString) to String

I ended up with the ugly line:

var uuidString:String = regionToMonitor["uuid"] as! String

no warnings, no errors, no runtime error

(key: AnyObject, value: AnyObject)' does not have a member named 'subscript'

'(key: AnyObject, value: AnyObject)' indicates that item is not an Dictionary but is a Tuple with a single key/value pair.

Iterating dictionaries in swift interates through tuples:

for (key, value) in json {
println(key, value)
}

Your for loop indicates that you are probably wanting a json Array of tuples instead of a json Dictionary.

item["id"] would give you a compile time error if you declared the parameter as a tuple. It seems you stumbled onto something hidden with the language with how either tuples or subscripts work under the hood.

More on Subscripts

More on Types (Tuples)

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

According to Swift Language Guide you are expected to get a warning when casting optional to Any (see note at the bottom of the page). You can get rid of warning by casting optional value to Any as shown below.

let email : String?;
let password : String?;
let dict = ["email": email as Any, "password": password as Any] as [String: Any];

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

It's less than perfect, but it works if the protocol and class are both @objc (and the class subclasses NSObject):

@objc protocol Nameable: class {
var name: String { get }
}

@objc class Person: NSObject, Nameable {
var name: String
init(name: String) {
self.name = name
}
}

...

var array: [Nameable] = [personOne, personTwo]
let array2 = array as [AnyObject] // ✓


Related Topics



Leave a reply



Submit