के लिए ऑब्जेक्ट्स के रूप में स्विफ्ट प्लिस्ट पढ़ना और प्रारंभ करना Arrays स्विफ्ट में एक छोटा UITableView
-आधारित ऐप विकसित कर रहा हूं। मैं बाहर शुरू कर दिया (जैसे कुछ बुनियादी सरणियों शामिल बुनियादी वर्ग घोषणा और UITableView
आउटलेट घोषणा हार्ड-कोड है:कुंजी
import UIKit
class ScenesViewController: UITableViewController {
@IBOutlet var myTableView: UITableView
var sceneName = ["Scene 1", "Scene 2", "Scene 3", "Scene 4", "Scene 5"]
var sceneDetail = ["Hi", "Hello", "Bye", "My Bad", "Happy"]
इस कोड को एक UITableViewController
जो एक आउटलेट है कि सभी एक UITableView
को झुका रहा है अंदर है। ऑब्जेक्टिव-सी में, मैं यह करना होगा मैं इस तथाकथित "Scenes.plist" के लिए एक plist बनाया है और कोड को बदलने का प्रयास किया है:।
NSString *path = [[NSBundle mainBundle] pathForResource:@"Scenes" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
sceneName = [dict objectForKey:@"SceneName"];
sceneDetail = [dict objectForKey:@"SceneDetail"];
मैं इस तरह स्विफ्ट में यह कर शुरू कर दिया
let path = NSBundle.mainBundle().pathForResource("Scenes", ofType:"plist")
let dict = NSDictionary(contentsOfFile:path) // Error: 'ScenesViewController.Type' does not have a member named 'path'
मैंने समाधान के लिए इंटरनेट को खराब कर दिया लेकिन एक खोजने में असमर्थ था। एकमात्र समाधान मैं लगभग एक लाइन पर प्रत्येक सरणी के लिए यह सब कर रहा है का सहारा कर सकते हैं
let dict = NSDictionary(contentsOfFile:
NSBundle.mainBundle().pathForResource("Scenes", ofType:"plist"))
var sceneName = dict["SceneName"] // Error: 'ScenesViewController.Type' does not have a member named 'dict'
अब,:
एक 'लगभग' एक लाइनर - तो मैं कार्रवाई के अगले पाठ्यक्रम शुरू कर दीvar sceneName = NSDictionary(contentsOfFile:
NSBundle.mainBundle().pathForResource("Scenes", ofType:"plist").objectForKey("SceneName")
// Warning: Variable 'sceneName' inferred to have type 'AnyObject!', which may be unexpected
// Fix-It: Add an explicit type annotation to silence this warning (var sceneName: AnyObject! = ...)
इसलिए मैंने यह पता लगाने के लिए स्पष्ट प्रकार की एनोटेशन जोड़ा कि अधिक त्रुटियां थीं। cellForRowAtIndexPath:
समारोह के अंदर:
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
let cell: UITableViewCell = tableView!.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
cell.textLabel.text = sceneName[indexPath!.row] // Error: could not find member 'row'
return cell
}
वास्तव दो त्रुटियाँ हैं; मुझे नहीं पता कि यह गलती से डुप्लिकेट है या नहीं। फिर
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
if segue?.identifier == "ShowDetails" {
var detailVC: ScenesDetailViewController = segue!.destinationViewController as ScenesDetailViewController
var myIndexPath: NSIndexPath = myTableView.indexPathForSelectedRow()
var row: Int = myIndexPath.row
detailVC.detailModal = [sceneDetail[row], sceneName[row]] // Error: Could not find an overload for 'subscript' that accepts the supplied arguments
}
}
, वहाँ दो त्रुटियाँ हैं;: अगले त्रुटि (रों) prepareForSegue:sender:
विधि में हैं हालांकि, मुझे विश्वास है कि ऐसा इसलिए है क्योंकि sceneDetail
और sceneName
दोनों का उपयोग किया जाता है। ऑब्जेक्टिव-सी में ऐसी फाइलों में प्लेलिस्ट पढ़ने और उपयोग करने के साथ-साथ वहां मौजूद त्रुटियों (या जो मैं नहीं आया) में जगहों में त्रुटियां प्रतीत होती हैं।
अग्रिम धन्यवाद,
-KodInc
'एनआईएसएनडीएक्सपीथ' की' पंक्ति' और 'खंड' गुणों को 'UIKit' में परिभाषित किया गया है, न कि' फाउंडेशन '। क्या आपकी फ़ाइल में 'आयात UIKit' जोड़ना इस समस्या को हल करता है? – Jack
@ जैकव्यू 'UIKit' पहले से ही आयात किया जा चुका है; यह पहली पंक्ति – KodInc