mosaic6 mosaic6 - 13 days ago 4
iOS Question

Binary operator '!=' cannot be applied to operands of type 'Bool' and 'NilLiteralConvertible'


Binary operator '!=' cannot be applied to operands of type 'Bool' and 'NilLiteralConvertible'


Getting error on if ((object.isKindOfClass(NSDictionary)) != nil) {

let paramString:NSMutableString = NSMutableString();
(obj as! NSArray).enumerateObjectsUsingBlock({ (object, idx, stop) -> Void in
if ((object.isKindOfClass(NSDictionary)) != nil){
let pair:NSDictionary? = object as? NSDictionary;
let textId:NSNumber? = pair?.objectForKey("TextId") as? NSNumber;
var content:NSString? = pair?.objectForKey("Content") as? NSString;

if ((content == nil) || (content?.length == 0) ) {
content = " ";
}

if ((textId != nil) && (content != nil)) {
paramString.appendFormat("%ld:%@\n", textId!.integerValue, content!);
}
}
});

Answer

Replace your line:

if ((object.isKindOfClass(NSDictionary)) != nil){

with this line:

if object.isKindOfClass(NSDictionary) {

Syntax for isKindOfClass is:

func isKindOfClass(aClass: AnyClass!) -> Bool

Means isKindOfClass will return Bool and you can not compare it with nil. Thats why compiler is giving you an error.