0
votes

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!);
                        }
                    }
                });
3
Please use only tags appropriate to each question. Your questions have nothing to do with the company Apple. And read the tag descriptions. The [apple] tag clearly states no to use it. - rmaddy

3 Answers

5
votes

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.

0
votes

isKindOfClass returns a Bool (a true or false value), not an optional. Bools can be directly tested in conditional statements (!= actually returns a Bool), so don't test for nil:

if object.isKindOfClass(NSDictionary) {
0
votes

object.isKindOfClass(NSDictionary)) will return a Bool (either true or false) depending if it is part of that class or not.

I've never used Swift before but I'm guessing you can't compare a Bool and a NilLiteralConvertible (nil) with !=. You should probably check if your object is nil and then check if object.isKindOfClass(NSDictionary)) returns false.