如何检查对象是否是一个对象数组

时间:2016-09-30 12:46:11

标签: arrays swift object

我有一个像下面的对象,它是一个对象数组。

在swift语言中,我如何检查对象是否是一个对象数组?

 DefinitionList =  (
          {
                    accountNum =          {
                        isEditable = 1;
                        isRequired = 1;  
                    };

                    bAccountType =           {
                        isEditable = 1;
                        isRequired = 0;
                   },

        },                                
        {
                    accountNum =     {
                        isEditable = 1;
                        isRequired = 1;
                    };

                    bAccountType =       {
                        isEditable = 1;
                        isRequired = 0;
                    };
                },

                ..
                ..)

2 个答案:

答案 0 :(得分:0)

通常我在Swift 2中使用它:

var  DefinitionList = NSObject?()

    DefinitionList = ["ciao" : "ciao"]

  //  DefinitionList = ["ciao"]

    guard DefinitionList != nil else {

        print("DefinitionList empty")

        return
    }

    guard ((DefinitionList as? Array<NSObject>) != nil)  else {

        print("I'm a Dictionary")
        return
    }

    print("I'm a Array")

Swift 3

   var  DefinitionList : NSObject?

  //  DefinitionList = ["ciao" : "ciao"] as NSObject

      DefinitionList = ["ciao"] as NSObject

    guard DefinitionList != nil else {

        print("DefinitionList empty")

        return
    }

    guard ((DefinitionList as? Array<NSObject>) != nil)  else {

        print("I'm a Dictionary")
        return
    }

    print("I'm a Array")

答案 1 :(得分:-1)

您可以在Swift语言中使用“is”运算符。

if objects is [AnyObject] {
    print("right, its array of objects!")
} else {
    print("no, its not an array of objects!")
}

希望这会对你有所帮助