UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Swift Array append woes

Forums > Swift

@Paul  

Hi everyone,

I'm running into a bit of weird issue around inserting and removing elemnts from an array.

According to the apple docs I would expect removeFirst and append to not cause any issues and just work how it should but obviously I'm missing something.

Most of the time the new elemnt placed with append is inserted at index 0, sometimes its inserted in the middle of the array, see example output.

Anyone have any ideas as to why this is happening?

var Rosters:[Roster]

public func addNewRandomRoster(){
        print("-------------")
        while Rosters.count > 6 {
            print("Rosters over 6, RC: \(Rosters.count)")
            Rosters.removeFirst(1)
            print("Removed Roster")
            print("RC: \(Rosters.count)")
        }
        let tmp = Roster(id: UUID(), roster: generateWatchRoster())
        Rosters.append(tmp)
        let i = Rosters.firstIndex(where: {$0.id == tmp.id})
        print("Roster inserted at index: \(i!)")
        print("Post Inser RC: \(Rosters.count)")

    }

Output:

-------------
Rosters over 6, RC: 7
Removed Roster
RC: 6
Roster inserted at index: 0
Post Inser RC: 7
-------------
Rosters over 6, RC: 7
Removed Roster
RC: 6
Roster inserted at index: 0
Post Inser RC: 7
-------------
Rosters over 6, RC: 7
Removed Roster
RC: 6
Roster inserted at index: 0
Post Inser RC: 7
-------------
Rosters over 6, RC: 7
Removed Roster
RC: 6
Roster inserted at index: 0
Post Inser RC: 7
-------------
Rosters over 6, RC: 7
Removed Roster
RC: 6
Roster inserted at index: 4
Post Inser RC: 7

The class does use swift data, I will try without it shorly to verify the behaviour.

1      

@Paul  

Yes, this behaviour is fine without swift data....

Anyone have any ideas what is going on in swiftdata land here?

I might try a ring buffer, but I was hoping to avoid the extra managment.

1      

SwiftData/CoreData stores the data a bit like a Set so when you @Query it it can come in any order! I used a computed property to sort it. (Just be careful when removing items).

2      

@Paul  

Legend, thanks for the info @NigelGee

1      

May I add a different comment?

Capitalise the names of Classes and Structs. But use camelCase for variable names.

// Here. Don't capitalise your var.
//var Rosters:[Roster]  // <- rosters is a collection of Roster elements.

// Instead
var rosters: [Roster]  // <- Typical convention 

It's a convention widely used in Swift development.

Keep Coding!

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.