従来のコアデータの道を:私は最後にブレークポイントを置くと、作成された結果を印刷した場合
func queryForContactDate(context:NSManagedObjectContext) -> AnyObject?
{
var expressionDescriptions = [AnyObject]();
let expressionDescription = NSExpressionDescription()
// Name the column
expressionDescription.name = "maxUpdated"
// Use an expression to specify what aggregate action we want to take and
// on which column. In this case max on the update_at column
expressionDescription.expression = NSExpression(format: "@max.updated_at")
// Specify the return type we expect
expressionDescription.expressionResultType = .DateAttributeType
// Append the description to our array
expressionDescriptions.append(expressionDescription)
// Build out our fetch request the usual way
let request = NSFetchRequest(entityName: Contact.entityName())
// Specify we want dictionaries to be returned
request.resultType = .DictionaryResultType
// Hand off our expression descriptions to the propertiesToFetch field.
request.propertiesToFetch = expressionDescriptions
// Our result is going to be an array of dictionaries.
var results:[[String:AnyObject]]?
// Perform the fetch. This is using Swfit 2, so we need a do/try/catch
do {
results = try context.executeFetchRequest(request) as? [[String:AnyObject]]
} catch _ {
// If it fails, ensure the array is nil
results = nil
}
return results![0];
}
最大値または最小値は、フェッチ制限値1で照会し、そのキーをソートすることです。このObjective-Cのコードのように:
+ (NSFetchRequest *) requestForStatusWithMaxID {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName: kAMStatusEntity];
NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey: kAMTwID ascending:NO];
request.sortDescriptors = @[sd];
request.fetchLimit = 1;
return request;
} // +requestForStatusWithMaxID
あなたのupdated_at
財産については、上記を変更するには、非常に簡単になります。
キャストなしでcontext.executeFetchRequest(request)で印刷できますか?実際に[[String:AnyObject]]ではないものをキャストしている可能性があります。特にSwiftに移植されているobjcのコーナーでは、辞書や配列に続く奇妙なことがあります – tbondwilkinson
あなたは正しい軌道にいると思われますが、印刷物は特に啓発的ではありませんでした。ここに私が得たものがあります:[{ }、{ maxUpdated = "2015-12-30 20:05:31 +0000"; }] – pbuchheit
私はfetchメソッドにブレークポイントを入れて、それをトレースして、どこにリクエストが出てきたか/空のディクショナリがそこに来るのを見ます。 – tbondwilkinson