我有一个通用函数,它接受符合Protocol Printable
的记录。
class Printer
{
func printRecords<T: Record where T: Printable>(records: [T], completion: ((e: NSError?) -> Void)? )
{
// do print stuff here
var data = NSMutableData()
for r in records
{
data.appendData(r.printableData)
}
let error = self.printData(data)
completion?(e: error)
}
}
以下是Record
和Printable
声明和实施:
protocol Printable
{
var printableData: NSData { get }
}
public class Record: NSManagedObject
{
// property declaration here
}
public class Person: Record
{
// property declaration here
}
public class Address: Record
{
// property declaration here
}
// implement Printabl
extension Address: Printable
{
var printData: NSData
{
// format print data here...
.
.
.
return data
}
}
但是当我调用这样的方法时:
// was initially inserted into coredata via
// NSEntityDescription.insertNewObjectForEntityForName("Address", inManagedObjectContext: context) as! Address
let address: Address = ...
// This line: Generic Parameter 'T' Could not be Inferred
printer.printRecords([address], completion: { (e: NSError?) in
// handle print completion here
})
我收到了错误:
无法推断通用参数'T'