无法将类型“ [Surah]”的值转换为预期的参数类型“ Range <Int>”

时间:2020-04-12 09:57:01

标签: json swiftui

嗨,伙计们 我试图学敏捷,我需要你的帮助 我尝试用快速连接json遇到错误

无法将类型“ [Surah]”的值转换为预期的参数类型“范围”

谢谢您的帮助

struct Surah: Codable {
struct Juz: Codable {
    let index: String
    let verse: Verse
}
struct Verse: Codable {
    let start, end: String
}
enum Place: String, Codable {
    case mecca = "Mecca"
    case medina = "Medina"
}
enum TypeEnum: String, Codable {
    case madaniyah = "Madaniyah"
    case makkiyah = "Makkiyah"
}



let place: Place
let type: TypeEnum
let count: Int
let title, titleAr, index, pages: String
let juz: [Juz] }

/ *************** /

struct ContentView: View {

let surahs: [Surah] = Bundle.main.decode("source/surah.json")
var body: some View {
    NavigationView{
        List{
            ForEach(surahs){section in
                Section(header:Text(section.title)){
                    ForEach(section.juz, id: \.verse){juzs in
                        Text(juzs.verse.start)
                    }
                }

            }

           }
    }

}

}

   {
    "place": "Mecca",
    "type": "Makkiyah",
    "count": 7,
    "title": "Al-Fatiha",
    "titleAr":"الفاتحة",
    "index": "001",
    "pages": "1",
    "juz": [
        {
            "index": "01",
            "verse": {
                "start": "verse_1",
                "end": "verse_7"
            }
        }
    ]
},
{
    "place": "Medina",
    "type": "Madaniyah",
    "count": 286,
    "title": "Al-Baqara",
    "titleAr":"البقرة",
    "index": "002",
    "pages": "2",
    "juz": [
        {
            "index": "01",
            "verse": {
                "start": "verse_1",
                "end": "verse_141"
            }
        },
        {
            "index": "02",
            "verse": {
                "start": "verse_142",
                "end": "verse_252"
            }
        },
        {
            "index": "03",
            "verse": {
                "start": "verse_253",
                "end": "verse_286"
            }
        }
    ]
}

我正在尝试为水平收集属性创建forEach方法

我怎么称呼这部分json ??

我希望有人能帮助我。

现在我需要在我的Swift中调用第二个Json部分

{
"index": "001",
"name": "al-Fatihah",
"verse": {
    "verse_1": "بِسْمِ ٱللَّهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ",
    "verse_2": "ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَٰلَمِينَ",
    "verse_3": "ٱلرَّحْمَٰنِ ٱلرَّحِيمِ",
    "verse_4": "مَٰلِكِ يَوْمِ ٱلدِّينِ",
    "verse_5": "إِيَّاكَ نَعْبُدُ وَإِيَّاكَ نَسْتَعِينُ",
    "verse_6": "ٱهْدِنَا ٱلصِّرَٰطَ ٱلْمُسْتَقِيمَ",
    "verse_7": "صِرَٰطَ ٱلَّذِينَ أَنْعَمْتَ عَلَيْهِمْ غَيْرِ ٱلْمَغْضُوبِ عَلَيْهِمْ وَلَا ٱلضَّآلِّينَ"
},
"count": 7,

"juz": [
    {
        "index": "01",
        "verse": {
            "start": "verse_1",
            "end": "verse_7"
        }
    }
]

}

如何调用和编码?或格式化

1 个答案:

答案 0 :(得分:0)

struct ContentView: View {

let surahs: [Surah] = Bundle.main.decode("source/surah.json")
var body: some View {
NavigationView{
    List{
        ForEach(surahs, id:\. index){section in
            Section(header:Text(section.title)){
                ForEach(section.juz, id: \.verse){juzs in
                    Text(juzs.verse.start)
                }
            }

        }

       }
}

}