我正在处理这个引用应用程序,我一直遇到两个错误,只是不想与我合作。它说“Type'businessQuote'没有成员('array'/'dict')”。在下面的屏幕截图中,您将看到该行上的错误。重点是让应用程序在提供的文本字段中显示随机引用。请你帮助我好吗?提前谢谢。
我的目标是让“ImportList”工作
如果还有其他一些我忽略的问题,如果你可以把我链接到它,我将不胜感激。但我真的需要一个答案。再次感谢你。
以下是包含错误的代码:
import Foundation
import UIKit
import Social
class businessQuote: UIViewController {
//============================//
//********** Outlets *********//
//============================//
let utility = Utility()
@IBOutlet weak var quoteDisplay: UILabel!
@IBOutlet weak var authorDisplay: UILabel!
@IBOutlet weak var quoteBackground: UIImageView! //GET BACK TO THIS
//============================//
//********** General *********//
//============================//
let date = NSDate()
var Author: String = ""
var Quote: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Checks if time is greater then 3pm to change background
let currentTime = utility.currentTime()
if (currentTime >= 15 ) {
quoteBackground.image = UIImage(named: "quote_background.png")
} else {
quoteBackground.image = UIImage(named:"morning_quote_background.png")
}
}
//============================//
//********* New Quote ********//
//============================//
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// Generates Random Number
func randomNumber(arrayLength: Int) -> Int {
let unsignedArrayCount = UInt32(arrayLength)
let unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
let randomNumber = Int(unsignedRandomNumber)
return randomNumber
}
// Importing Quotes plist File
let businessQuotes = ImportList(FileName: "BusinessList")
// Selects Quote
let chosenQuote: String = businessQuote.array[randomNumber(businessQuote
.count())] as! String
let chosenAuthor = businessQuote.dict[chosenQuote]! as String
// Assigns Quote & Author to IBOutlet
Author = chosenAuthor
Quote = chosenQuote
quoteDisplay.text = Quote
authorDisplay.text = Author.uppercaseString
}
}
这是带有'array'和'dict'的代码
import Foundation
struct ImportList {
let path: String
init(FileName: String) {
self.path = NSBundle.mainBundle().pathForResource("\(FileName)", ofType: "plist")!
}
var dict: Dictionary<String, String> {
return NSDictionary(contentsOfFile: path)! as! Dictionary
}
var array: Array<AnyObject> {
return [String](arrayLiteral: String(dict.keys) { $0 as AnyObject as! String })
}
func count() -> Int {
return array.count
}
}
谢谢!
答案 0 :(得分:1)
您已将变量 businessQuotes 声明为:
// Importing Quotes plist File
let businessQuotes = ImportList(FileName: "BusinessList")
但是,使用 businessQuote 代替您,看到您缺少&#34; s&#34;在末尾。拼写错误。以下行应该是:
// Selects Quote
let chosenQuote: String = businessQuotes.array[randomNumber(businessQuotes
.count())] as! String
let chosenAuthor = businessQuotes.dict[chosenQuote]! as String