每当我尝试编译我的作业时,我都会收到此错误
(unresolved_dot_expr type='@lvalue [Book]' location=/Users/BandManFabo/Desktop/Lab03/Lab03/Bookstore.swift:18:9 range=[/Users/BandManFabo/Desktop/Lab03/Lab03/Bookstore.swift:18:9 - line:18:9] field 'books' function_ref=unapplied
我正在使用一个名为Book的类,它有点像Bookstore中的书籍模型,这是另一个类。 Book.swift位于
之下import Foundation
class Book {
private var title:String
private var numOfPages:Int
private var price: Double
private var quantity: Int
init(title:String,pages:Int,price:Double,quantity:Int) {
self.title = title
self.numOfPages = pages
self.price = price
self.quantity = quantity
}
public func getTitle() -> String {
return self.title
}
public func getPrice() -> Double {
return self.price
}
public func getQuantity() -> Int {
return self.quantity
}
public func toString() -> String {
return "Title: \(self.title)\nNumber Of Pages: \(self.numOfPages)\nPrice: \(self.price)\nQuantity: \(self.quantity)"
}
public func subtractQuantity(amount: Int){
self.quantity = getQuantity() - amount
}
public func addQuantity(amount: Int){
self.quantity = getQuantity() + amount
}
}
Bookstore使用此类运行
import Foundation
class Bookstore {
private var totalbooks:Int
private var gross: Double
private static let MAXNUMOFBOOKS:Int = 1000
private var books: [Book]
//empty constructor for the bookstore
init() {
books = [Book](repeatElement(Book, count: Bookstore.MAXNUMOFBOOKS))
totalbooks = 0
gross = 0.0
}
/* Adds a new book to this bookstore.
* @param b the book to add
*/
public func addBook(b:Book){
if Bookstore.MAXNUMOFBOOKS < books.count{
books.append(b)
totalbooks = totalbooks+1
}else{
print("\nBookStore: I cannot add a new book into stock.")
}
}
/**
* Adds a certain quantity of a book already in stock.
*
* @param title the title of the the book
* @param quantity amount of copies to add
*/
public func addBookQuantity(title:String , quantity:Int){
// Search for the book...if found adjust the quantity,
// otherwise, inform the user that the book is not available
for currentBook in books {
if currentBook.getTitle() == title{
currentBook.addQuantity(amount: quantity)
return
}//end of if
}//end of for
// Book is not found in the bookstore
print("\nBookStore: I cannot increment the quantity of the book titled");
print("'\(title)' because it is not available in the bookstore.");
}// addBookQuantity
/**
* Checks if at least a certain number of copies of a particular book are in
* stock. Note: You can use this method to check if a book is already in the
* bookstore. This way, you won't create duplicate records of the same book.
*
* @param title the title of the book to search for
* @param quantity the desired number of copies
* @return
* @returns true if title exists with specified quantity; otherwise false
*/
public func inStock(title:String, quantity:Int) -> Bool {
// Search for the book...if found, adjust the quantity.
// otherwise, Book not in the BookStore.
for currentBook in books {
if currentBook.getTitle() == title{
if quantity <= currentBook.getQuantity(){
return true
}else{
return false
}
}//end of if
}//end of for
//Book not present
return false
}
/**
* Sells a particular number of copies of a certain book. If successful
* (i.e. enough books are in stock to sell), the quantity of the book is
* adjusted. Otherwise, no books are sold.
*
* @param title the title of the book to sell
* @param quantity the amount of books to sell
* @return
* @returns true if successful; otherwise false
*/
public func sellBook(title:String, quantity:Int) -> Bool {
var sellCheck: Bool = false
//will check to see if the books are instock
let retval:Bool = inStock(title: title, quantity: quantity)
//will do some operation if it is instock
if retval {
for currentBook in books {
if !sellCheck{
if currentBook.getTitle() == title{
currentBook.subtractQuantity(amount: quantity)
gross = gross + currentBook.getPrice() * Double(quantity)
sellCheck = true
}//end of most inner if
}//end of inner if
}//end of outer for
}//end of outer if
return retval
}
/**
* Lists information about each book in the bookstore
*/
public func listBooks(){
print("\nAll Books In Store and Info\n===============")
for currentBook in books {
print(currentBook.toString())
}
}
/**
* Lists the titles of all the books in the bookstore
*/
public func listTitles(){
// List all books titles
print("\nTitles of Books\n===============")
for currentBook in books {
print(currentBook.getTitle())
}
}
/**
* Returns the gross income of this bookstore.
*
* @return
* @returns gross income
*/
public func getIncome() -> Double {
return gross
}
}
不完全确定分段错误的来源。我感谢任何帮助
答案 0 :(得分:0)
修改BookStore类中的init方法
amqplib-easy