Match the Data Type of a Object in Swift

Match the data type of a object in Swift

change var xyz : AnyObject to var xyz : Any and add it will match to this case

case let x as Int:

from REPL

  1> var a : Any = 1
a: Int = <read memory from 0x7fec8ad8bed0 failed (0 of 8 bytes read)>
2> switch a { case let x as Int: println("int"); default: println("default"); }
int

from The Swift Programming Language

You can use the is and as operators in a switch statement’s cases to
discover the specific type of a constant or variable that is known
only to be of type Any or AnyObject. The example below iterates over
the items in the things array and queries the type of each item with a
switch statement. Several of the switch statement’s cases bind their
matched value to a constant of the specified type to enable its value
to be printed:

for thing in things {
switch thing {
case 0 as Int:
println("zero as an Int")
case 0 as Double:
println("zero as a Double")
case let someInt as Int:
println("an integer value of \(someInt)")
case let someDouble as Double where someDouble > 0:
println("a positive double value of \(someDouble)")
case is Double:
println("some other double value that I don't want to print")
case let someString as String:
println("a string value of \"\(someString)\"")
case let (x, y) as (Double, Double):
println("an (x, y) point at \(x), \(y)")
case let movie as Movie:
println("a movie called '\(movie.name)', dir. \(movie.director)")
default:
println("something else")
}
}

// zero as an Int
// zero as a Double
// an integer value of 42
// a positive double value of 3.14159
// a string value of "hello"
// an (x, y) point at 3.0, 5.0
// a movie called 'Ghostbusters', dir. Ivan Reitman

Note:

var xyz : AnyObject = 1

will give you NSNumber because Int is not object so it auto convert it to NSNumber which is object

How do you find out the type of an object (in Swift)?

Swift 3 version:

type(of: yourObject)

Checking if an object is a given type in Swift

If you want to check against a specific type you can do the following:

if let stringArray = obj as? [String] {
// obj is a string array. Do something with stringArray
}
else {
// obj is not a string array
}

You can use "as!" and that will throw a runtime error if obj is not of type [String]

let stringArray = obj as! [String]

You can also check one element at a time:

let items : [Any] = ["Hello", "World"]
for obj in items {
if let str = obj as? String {
// obj is a String. Do something with str
}
else {
// obj is not a String
}
}

Swift: Test class type in switch statement

You absolutely can use is in a switch block. See "Type Casting for Any and AnyObject" in the Swift Programming Language (though it's not limited to Any of course). They have an extensive example:

for thing in things {
switch thing {
case 0 as Int:
println("zero as an Int")
case 0 as Double:
println("zero as a Double")
case let someInt as Int:
println("an integer value of \(someInt)")
case let someDouble as Double where someDouble > 0:
println("a positive double value of \(someDouble)")
// here it comes:
case is Double:
println("some other double value that I don't want to print")
case let someString as String:
println("a string value of \"\(someString)\"")
case let (x, y) as (Double, Double):
println("an (x, y) point at \(x), \(y)")
case let movie as Movie:
println("a movie called '\(movie.name)', dir. \(movie.director)")
default:
println("something else")
}
}

Compare two instances of an object in Swift

Indicate that your class conforms to the Equatable protocol, and then implement the == operator.

Something like this:

class PLClient: Equatable 
{
var name = String()
var id = String()
var email = String()
var mobile = String()
var companyId = String()
var companyName = String()
//The rest of your class code goes here

public static func ==(lhs: PLClient, rhs: PLClient) -> Bool{
return
lhs.name == rhs.name &&
lhs.id == rhs.id &&
lhs.email == rhs.email &&
lhs.mobile == rhs.mobile &&
lhs.companyId == rhs.companyId &&
lhs.companyName == rhs.companyName
}
}

For-in loop and type casting only for objects which match type

You can use a for-loop with a case-pattern:

for case let item as YourType in array {
// `item` has the type `YourType` here
// ...
}

This will execute the loop body only for those items in the
array which are of the type (or can be cast to) YourType.

Example (from
Loop through subview to check for empty UITextField - Swift):

for case let textField as UITextField in self.view.subviews {
if textField.text == "" {
// ...
}
}

RLMException: Object type does not match RLMArray type

Realm Lists are not covarient. You can only store objects of exactly the declared type in them.

SwiftyJSON - is possible to check object type?

The JSON objects in SwiftyJSON have a type property whose type is an enum

public enum Type: Int {
case number
case string
case bool
case array
case dictionary
case null
case unknown
}

For example

var list = json["OBJECT"]
switch list.type {
case .array: print("list is Array")
case .dictionary: print("list is Dictionary")
default: break
}


Related Topics



Leave a reply



Submit