1

I made a CustomStruct which it makes own customID, I could use this customID as id for ForEach in item mode, then I tried to use my own customID as id for ForEach in indices mode, but it gives error, It says Int has no member of customID, which my all reason of making customID was and is to not using Int as id, but using my own customID. How could I solve this issue?

    struct ContentView: View {
    
    @State var arrayOfCustomStruct: [CustomStruct] = [CustomStruct]()
    
    var body: some View {
        
        Spacer()
        
//        VStack {
//
//            ForEach (arrayOfCustomStruct, id: \.customID) { item in
//
//                Text( String(item.customID) )
//
//            }
//
//        }
        
        
        VStack {
            
            ForEach (arrayOfCustomStruct.indices, id:\.self) { index in // << here I want this happen: ----->  id: \.customID
                
                Text( String(arrayOfCustomStruct[index].customID) )
                
            }
            
        }
        
        Spacer()
        
        HStack {
            
            Spacer()
            
            Button("add new item") {
                
                arrayOfCustomStruct.append( CustomStruct(name: "New Item")   )
                
            }
            
            Spacer()
            
            Button("remove an item") {
                
                if arrayOfCustomStruct.count > 0 {
                    arrayOfCustomStruct.remove(at: arrayOfCustomStruct.count-1 )
                }
                
            }
            
            Spacer()
            
        }.padding()
        
    }
}


struct CustomStruct {
    
    init(name: String) {
        CustomStruct.indexOfId += 1
        self.name = name
    }
    
    private static var indexOfId: UInt64 = 0
    let customID: UInt64 = CustomStruct.indexOfId
    var name: String
    
}

Update: customID in Int has completely deferent personality than customID in CustomStruct! It is NOT perfect as CustomStruct.

    extension Int {

    private static var indexOfId: Int = -1
    var customID: Int {
        
        Int.indexOfId += 1
        return Int.indexOfId
    }
    
}
1
  • have you read my title? Custom! UUID is not Custom. Commented Dec 18, 2020 at 6:14

1 Answer 1

1

If you want to have both indices and id by item, you can use enumerated, like

ForEach (Array(arrayOfCustomStruct.enumerated()), id:\.1.customID) { index, item in 
    
    Text( String(arrayOfCustomStruct[index].customID) )
    
}
Sign up to request clarification or add additional context in comments.

4 Comments

It is working, but how could I just use index? Here is my idea, let me know if I am wrong or right? When we are just using item mode of ForEach, id goes and find our customID and then it syncing itself to that! But when we use just index mode, ForEach does not access to our customID, because it just care about Range! And Since Range is an Int type it try to go find customID inside of Int to syncing itself! but it can not find it! because customID is property of CustomStruct not Int. Is it right until now?
with knowing this also that struct cannot inherit from each other, then there is no way using just index mode of ForEach with customID, right?
Well... probably you've got stuck with term "mode" - there is no any "mode", ForEach just iterates with collection and use id as a property of element from collection. So when you use arrayOfCustomStruct.indices you don't iterate arrayOfCustomStruct in indices mode, you just iterate over range of Ints, so id can be only that available for Int. (Of course you can add extension to Int with calculable property and use it as ID, but this does not change the sense).
@ Asperi: U just said all what I said in right syntax, also I just used your advice about extension! Now Int have access to customID. check my Update, it works in index mode as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.