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

Posted

技术标签:

【中文标题】无法将类型“[Surah]”的值转换为预期的参数类型“Range<Int>”【英文标题】:Cannot convert value of type '[Surah]' to expected argument type 'Range<Int>' 【发布时间】:2020-04-12 09:57:01 【问题描述】:

大家好 我试着学快,我需要你的帮助 我尝试用 swift 连接 json 我得到错误

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

谢谢你的帮助

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)
                        
                    
                
            
        
    

JSON:

   [
    "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"
        
    
]

如何调用它并对其进行编码?或格式化

【问题讨论】:

您需要将SurahIdentifiable 一致,或者在ForEach 中明确使用id: 我得到 Surah Identifiable und 那行得通, 我对你有更多疑问,如何在不更改 Surah 的情况下直接在 ForEach 中使用 id:?谢谢你的帮助 【参考方案1】:
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)
                
            

        

       



【讨论】:

以上是关于无法将类型“[Surah]”的值转换为预期的参数类型“Range<Int>”的主要内容,如果未能解决你的问题,请参考以下文章

无法将 Float 类型的值转换为预期的参数类型 NSNumber

无法将“testViewController.Type”类型的值转换为预期的参数类型“UIViewController”

无法将类型“()”的值转换为预期的参数类型“字符串”

无法将“字符串”类型的值转换为预期的参数类型 [任何]

Swift:无法将“NSDate”类型的值转换为预期的参数类型“NSDateComponents”

无法将“()”类型的值转换为预期的参数类型“(() -> Void)?”在 swiftui