Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 是否要检查数组中是否存在对象_Arrays_Swift_Object - Fatal编程技术网

Arrays 是否要检查数组中是否存在对象

Arrays 是否要检查数组中是否存在对象,arrays,swift,object,Arrays,Swift,Object,例如,我的数组如下所示 data.json Questions("Question":"KEK1" , "Answers": ["", "", "", ""], "Answer": 1) Questions("Question":"KEK2" , "Answers": ["", "", "", ""], "Answer": 1) Questions("Question":"KEK3" , "Answers": ["", "", "", ""], "Answer": 1) Questions("Q

例如,我的数组如下所示

data.json

Questions("Question":"KEK1" , "Answers": ["", "", "", ""], "Answer": 1)
Questions("Question":"KEK2" , "Answers": ["", "", "", ""], "Answer": 1)
Questions("Question":"KEK3" , "Answers": ["", "", "", ""], "Answer": 1)
Questions("Question":"BAB1" , "Answers": ["", "", "", ""], "Answer": 1)
Questions("Question":"BAB2" , "Answers": ["", "", "", ""], "Answer": 1)
Questions("Question":"BAB3" , "Answers": ["", "", "", ""], "Answer": 1)
mycode:

这是我使用red从data.json中提取数据并将它们放入一个名为Questions的数组中的代码

struct Question {
    var Question: String!
    var Answers: [String]!
    var Answer: Int!

    init(item: [String: Any])
    {
        self.Question = item["Question"] as? String
        self.Answers = item["Answers"] as? [String]
        self.Answer = item["Answer"] as? Int
    }
}

class LittleTestViewController: UIViewController {

var Questions = [Question]()
override func viewDidLoad() {
        super.viewDidLoad()
  jsonParsingQuestionsFile()
}

//parsing fata from json file
    func jsonParsingQuestionsFile ()
    {
        guard let path = Bundle.main.path(forResource: "data", ofType: "json"),
            let array = (try? JSONSerialization.jsonObject(with: Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe), options: JSONSerialization.ReadingOptions.allowFragments)) as? [[String : Any]] else{
                return
        }
        for item in array
        {
            self.Questions.append(Question(item: item))
        }
    }
}
我想看看数组中是否存在一些对象,然后做些什么。 例如:

var Questions = [Question]()
var kek = "KEK"
var bab = "BAB"

if kek exists on Questions
{
// do something
}else if bab exists on Questions
{
// do something
}else
{
// end
}

我猜是类似于
问题。包含…

您可能正在寻找
过滤器。为了举例说明,我将大大简化。假设此结构:

struct Question {
    let q: String
}
然后假设这个数组:

let questions = [Question(q:"KEK1"), Question(q:"KEK2"), 
    Question(q:"KEK3"), Question(q:"KEK4"), Question(q:"KEK5"), 
    Question(q:"KEK6"), Question(q:"KEK7"), Question(q:"KEK8"), 
    Question(q:"KEK9"), Question(q:"KEK10"), Question(q:"KEK11"), 
    Question(q:"BAB1"), Question(q:"BAB2"), Question(q:"BAB3"), 
    Question(q:"BAB4"), Question(q:"BAB5"), Question(q:"BAB6"), 
    Question(q:"BAB7"), Question(q:"BAB8"), Question(q:"BAB9"), 
    Question(q:"BAB10"), Question(q:"BAB11")]
现在让我们假设问题是:从这个数组中,只得到KEK问题。我们可以使用
过滤器

let questionsWithKek = questions.filter{$0.q.contains("KEK")}
// result is: [Question(q: "KEK1"), Question(q: "KEK2"), Question(q: "KEK3"), 
//    Question(q: "KEK4"), Question(q: "KEK5"), Question(q: "KEK6"), 
//    Question(q: "KEK7"), Question(q: "KEK8"), Question(q: "KEK9"), 
//    Question(q: "KEK10"), Question(q: "KEK11")]
然后循环通过
问题swithkek
并对每一个问题都做些事情是很平常的


如果问题真的是原始的
questions
数组是否包含任何KEK问题,那么只需查看
questionsWithKek
是否为空。

什么是
myArray
?这显然不是一个数组。这些标签是什么问题,答案,答案?这是某种自定义对象类型吗?如果是的话,展示出来。另外,请使用小写字母表示变量名,大写字母表示类型名。这是一个json文件。。我将数据从json文件解析到myArrayThen,然后显示json。现在我不知道myArray应该是什么样子。您当前显示的肯定不是数组。您仍然没有执行我要求您执行的操作。你可以把它叫做数组,但它不是数组。显示解析并显示myArray的类型声明。编辑检查它是否工作并进行筛选,但我有KEK1、KEK2、…、KEK9、KEK10,它只指向KEK1 KEK2 KEK4、KEK4、KEK4、KEK8。它没有找到9和10。我对
bab
做了同样的操作,但它没有找到所有的bab,它只找到
bab1
。我不知道你在做什么,因为你仍然没有提供一致的测试输入和期望的输出。我向你保证,我的回答与我描述的一模一样。