Swift 3 从Plist中获取所有数组的全部元素
Solution: Combine All Plist Key Elements When Index is 0
To make your function return all elements from every key when muscleIndex is 0, you'll need to iterate over all target keys, fetch their respective arrays, and combine them into a single array. Here's how to modify your code effectively:
Modified Robust Code
func loadPlist() -> [[String: String]] { // Safely load the plist and cast to a native Swift dictionary guard let path = Bundle.main.path(forResource: "myText", ofType: "plist"), let dict = NSDictionary(contentsOfFile: path) as? [String: [[String: String]]] else { return [] // Return empty array if plist fails to load (avoids crashes) } if muscleIndex == 0 { // Collect all elements from every key into one array let allTargetKeys = ["abs", "arms", "back", "chest", "shoulders", "legs"] var combinedElements: [[String: String]] = [] for key in allTargetKeys { if let keyElements = dict[key] { combinedElements.append(contentsOf: keyElements) } } return combinedElements } else { // Use a switch statement for cleaner index-to-key mapping let targetKey: String? = switch muscleIndex { case 1: "abs" case 2: "arms" case 3: "back" case 4: "chest" case 5: "shoulders" case 6: "legs" default: nil } return targetKey.flatMap { dict[$0] } ?? [] } }
Key Changes & Improvements
- Safe Unwrapping: Replaced risky force unwraps (
!) withguard letand optional binding to prevent crashes if the plist is missing or malformed. - Combined Array for Index 0: When the user taps the index 0 cell, we loop through all muscle group keys, collect their elements, and merge them into a single array.
- Cleaner Index Mapping: Swapped the long
else ifchain for aswitchstatement, making the code easier to read and maintain. - Graceful Fallbacks: Returns an empty array for unexpected indices or missing keys instead of crashing.
Stripped-Down Version (Matching Original Force Unwrap Style)
If you prefer to keep your original code structure with force unwraps (not recommended for production), here's the minimal fix:
func loadPlist()->[[String:String]] { let path = Bundle.main.path(forResource: "myText", ofType: "plist")! let dict = NSDictionary(contentsOfFile: path!)! if muscleIndex == 0{ let allKeys = ["abs", "arms", "back", "chest", "shoulders", "legs"] var combinedArray: [[String:String]] = [] for key in allKeys { let array = dict.object(forKey: key) as! [[String:String]] combinedArray.append(contentsOf: array) } return combinedArray } else if muscleIndex == 1{ return dict.object(forKey: "abs") as! [[String : String]] } else if muscleIndex == 2{ return dict.object(forKey: "arms") as! [[String : String]] } else if muscleIndex == 3{ return dict.object(forKey: "back") as! [[String : String]] } else if muscleIndex == 4{ return dict.object(forKey: "chest") as! [[String : String]] } else if muscleIndex == 5{ return dict.object(forKey: "shoulders") as! [[String : String]] } else if muscleIndex == 6{ return dict.object(forKey: "legs") as! [[String : String]] } else { return [] } }
Either version will work, but the first one is more robust for real-world applications. When the index 0 cell is tapped, the function will now return all elements from every key in your plist.
内容的提问来源于stack exchange,提问作者IMan Merzaie
相关产品推荐
相关产品推荐

