123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- //
- // NYAccountManager.swift
- // JiaPeiManage
- //
- // Created by Ning.ge on 2023/7/28.
- //
- import Foundation
- struct Account {
- let username: String
- let password: String
- }
- class NYAccountManager {
- private var accounts: [Account] = []
- private let maxAccountCount = 5
-
- func addAccount(username: String, password: String) {
- let account = Account(username: username, password: password)
- if accounts.count < maxAccountCount {
- accounts.append(account)
- } else {
- accounts.removeFirst()
- accounts.append(account)
- }
- saveAccounts()
- }
-
- func deleteAccount(at index: Int) {
- guard index >= 0 && index < accounts.count else {
- return
- }
- accounts.remove(at: index)
- saveAccounts()
- }
-
- func getAccounts() -> [Account] {
- return accounts
- }
-
- private func saveAccounts() {
- // Convert the accounts array to a JSON representation and save it to UserDefaults or any other persistent storage of your choice
- let jsonEncoder = JSONEncoder()
- // if let data = try? jsonEncoder.encode(accounts) {
- // UserDefaults.standard.set(data, forKey: "savedAccounts")
- // }
- }
-
- private func loadAccounts() {
- // Load the accounts from UserDefaults or any other persistent storage
- // if let data = UserDefaults.standard.data(forKey: "savedAccounts") {
- // let jsonDecoder = JSONDecoder()
- // if let savedAccounts = try? jsonDecoder.decode([Account].self, from: data) {
- // accounts = savedAccounts
- // }
- // }
- }
-
- init() {
- loadAccounts()
- }
- }
|