如何检查给定的字典键(API数据)是否为Int:swift类型

时间:2016-12-13 05:08:28

标签: swift loops dictionary key

这是myDictionary从API调用回复:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">

<TextView
    android:id="@+id/list_item_string"
    tools:text="asifgiouweifhsklajfhkh"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:gravity="center"
    android:paddingLeft="8dp"
    android:textSize="18sp"
    android:textStyle="bold"
    android:layout_toStartOf="@+id/quantityView"/>

<Button
    android:id="@+id/delete_btn"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="5dp"
    android:text="??" />

<TextView
    android:text="QO"
    android:gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignBottom="@+id/list_item_string"
    android:layout_alignEnd="@+id/delete_btn"
    android:layout_marginEnd="80dp"
    android:id="@+id/unitView"/>

<TextView
    android:text="1234"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/quantityView"
    android:layout_alignBottom="@+id/unitView"
    android:layout_toStartOf="@+id/unitView"
    android:layout_alignParentTop="true"/>

如何检查词典“键”是否可用,以及它们是否为Int类型。如何检查给定的数据是否可以在表格视图中显示,只有 1,2,3,4,5,6的键以及更多的增加顺序,因为上面的字典可以进一步钥匙&amp;&amp;字典 排除其他密钥 (asAbove:key:f,s&amp; p)

任何人都可以推荐我任何例子..

1 个答案:

答案 0 :(得分:1)

     let keys = demo["data"]!["table"]!.keys

     for k in keys{
       var num = Int(k)
       if num != nil {
         print("Valid Integer")
       }
       else {
        print("Not Valid Integer")
      }
    }