我如何查看()的内容

时间:2022-03-18 07:15:58

Currently I'm using alamofire to fetch data from the server. The API will return either

目前我正在使用alamofire从服务器获取数据。 API将返回

My goal is to check whether the data is empty or not

我的目标是检查数据是否为空

Empty bracket

[()]

or will return

或将返回

[(
  {
    "name": "joker",
    "age": 12,  
  }

)]

if I do data[0] then it will show (), how do i check if the () is empty or have data

如果我做数据[0]然后它会显示(),我如何检查()是否为空或有数据

4 个解决方案

#1


0  

If yu are sure your data will be enclosed within [()], you can simply trim the string

如果你确定你的数据将被包含在[()]中,你可以简单地修剪字符串

let trimmedString = dataString.trimmingCharacters(in: CharacterSet(charactersIn: "[()]"))

and check if the trimmedString is empty.

并检查trimmedString是否为空。

#2


0  

Just use data.count to check empty. I think you used po in console, it will print extra ().

只需使用data.count检查为空。我认为你在控制台中使用了po,它会打印extra()。

#3


0  

When your data is empty, i.e. [()], then data.count will return 1 because it contains a single element that is empty.

当您的数据为空时,即[()],则data.count将返回1,因为它包含一个空的单个元素。

To check for if the contained element is also empty, you can use:

要检查包含的元素是否也为空,您可以使用:

data.first?.isEmpty

isEmpty can be used on any sequence type. It returns true is the sequence is empty, and false otherwise.

isEmpty可用于任何序列类型。返回true表示序列为空,否则返回false。

#4


0  

It seems not a valid JSON

它似乎不是一个有效的JSON

Anyways try this:

无论如何试试这个:

    /// <dataResponse> is your response sting...
    let charSet = CharacterSet.init(charactersIn: "[()]")
    let trimmedString = dataResposne.trimmingCharacters(in: charSet)
    if trimmedString.isEmpty {
        /// Empty Response
    }
    else    {
        /// Non Emtpy Response
    }

#1


0  

If yu are sure your data will be enclosed within [()], you can simply trim the string

如果你确定你的数据将被包含在[()]中,你可以简单地修剪字符串

let trimmedString = dataString.trimmingCharacters(in: CharacterSet(charactersIn: "[()]"))

and check if the trimmedString is empty.

并检查trimmedString是否为空。

#2


0  

Just use data.count to check empty. I think you used po in console, it will print extra ().

只需使用data.count检查为空。我认为你在控制台中使用了po,它会打印extra()。

#3


0  

When your data is empty, i.e. [()], then data.count will return 1 because it contains a single element that is empty.

当您的数据为空时,即[()],则data.count将返回1,因为它包含一个空的单个元素。

To check for if the contained element is also empty, you can use:

要检查包含的元素是否也为空,您可以使用:

data.first?.isEmpty

isEmpty can be used on any sequence type. It returns true is the sequence is empty, and false otherwise.

isEmpty可用于任何序列类型。返回true表示序列为空,否则返回false。

#4


0  

It seems not a valid JSON

它似乎不是一个有效的JSON

Anyways try this:

无论如何试试这个:

    /// <dataResponse> is your response sting...
    let charSet = CharacterSet.init(charactersIn: "[()]")
    let trimmedString = dataResposne.trimmingCharacters(in: charSet)
    if trimmedString.isEmpty {
        /// Empty Response
    }
    else    {
        /// Non Emtpy Response
    }