我将如何为这些 WatchKit 复杂功能创建模型?
How would I create a model for these WatchKit Complications?
我观看了 Creating Complications with ClockKit 著名 Apple 工程师 Eliza Block 在 WWDC 上的演示,并在她进行时对整个项目进行了编码。顺便说一句,很棒的教程。真的很彻底。
她展示了界面,但没有透露她的足球比赛迷你模型的实现。她可能认为这非常容易,所以没有表现出来。然而,作为一名新程序员,我希望看到一些实施示例以及它是如何完成的。
我已经包含了 ComplicationController class 和 SoccerMatch 模型接口。我尝试使用 NSDateComponents 创建日期,但 Xcode 警告我未使用的变量 'date'。此外,日期 @属性 已由 Apple 在界面中创建。实现中要写什么才能得到完整的模型?
import ClockKit
let MatchDuration = NSTimeInterval(60 * 90)
class ComplicationController: NSObject, CLKComplicationDataSource {
// MARK: - Timeline Configuration
func getSupportedTimeTravelDirectionsForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimeTravelDirections) -> Void) {
handler([.Forward, .Backward])
}
func getTimelineStartDateForComplication(complication: CLKComplication, withHandler handler: (NSDate?) -> Void) {
let startDate = timelineEntryDateForMatch(SoccerMatch.firstMatch())
handler(startDate)
}
func getTimelineEndDateForComplication(complication: CLKComplication, withHandler handler: (NSDate?) -> Void) {
let endDate = (SoccerMatch.lastMatch().date.dateByAddingTimeInterval(MatchDuration))
handler(endDate)
}
func getPrivacyBehaviorForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationPrivacyBehavior) -> Void) {
handler(.ShowOnLockScreen)
}
// MARK: - Timeline Population
func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: ((CLKComplicationTimelineEntry?) -> Void)) {
getTimelineEntriesForComplication(complication, beforeDate: NSDate(), limit: 1) { (entries) -> Void in
handler(entries?.first)
}
}
func getTimelineEntriesForComplication(complication: CLKComplication, beforeDate date: NSDate, limit: Int, withHandler handler: (([CLKComplicationTimelineEntry]?) -> Void)) {
var entries = [CLKComplicationTimelineEntry]()
var match : SoccerMatch? = SoccerMatch.lastMatch()
while let thisMatch = match {
let thisEntryDate = timelineEntryDateForMatch(thisMatch)
if date.compare(thisEntryDate) == .OrderedDescending {
let tmpl = templateForMatch(thisMatch)
let entry = CLKComplicationTimelineEntry(date: thisEntryDate, complicationTemplate: tmpl)
entries.append(entry)
if entries.count == limit { break }
}
match = match?.previousMatch()
}
handler(entries)
}
func getTimelineEntriesForComplication(complication: CLKComplication, afterDate date: NSDate, limit: Int, withHandler handler: (([CLKComplicationTimelineEntry]?) -> Void)) {
var entries = [CLKComplicationTimelineEntry]()
var match : SoccerMatch? = SoccerMatch.firstMatch()
while let thisMatch = match {
let thisEntryDate = timelineEntryDateForMatch(thisMatch)
if date.compare(thisEntryDate) == .OrderedAscending {
let tmpl = templateForMatch(thisMatch)
let entry = CLKComplicationTimelineEntry(date: thisEntryDate, complicationTemplate: tmpl)
entries.append(entry)
if entries.count == limit { break }
}
match = match?.nextMatch()
}
handler(entries)
}
// MARK: - Update Scheduling
func getNextRequestedUpdateDateWithHandler(handler: (NSDate?) -> Void) {
handler(nil);
}
// MARK: - Placeholder Templates
func getPlaceholderTemplateForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTemplate?) -> Void) {
let tmpl = CLKComplicationTemplateModularLargeStandardBody()
let soccerBall = UIImage(named: "soccer_ball")!
tmpl.headerImageProvider = CLKImageProvider(onePieceImage: soccerBall)
tmpl.headerTextProvider = CLKSimpleTextProvider(text: "Match schedule")
tmpl.body1TextProvider = CLKSimpleTextProvider(text: "2015 Women's Tournament")
handler(tmpl)
}
private func templateForMatch(match: SoccerMatch) -> CLKComplicationTemplate {
let tmpl = CLKComplicationTemplateModularLargeStandardBody()
let soccerBall = UIImage(named: "soccer_ball")!
tmpl.headerImageProvider = CLKImageProvider(onePieceImage: soccerBall)
tmpl.headerTextProvider = CLKTimeTextProvider(date: match.date)
tmpl.body1TextProvider = CLKSimpleTextProvider(text: match.teamDescription)
tmpl.body2TextProvider = CLKSimpleTextProvider(text: match.groupDescription)
return tmpl
}
private func timelineEntryDateForMatch(match: SoccerMatch) -> NSDate {
if let previousMatch = match.previousMatch() {
return previousMatch.date.dateByAddingTimeInterval(MatchDuration)
} else {
return match.date.dateByAddingTimeInterval(-6 * 60)
}
}
SoccerMatch.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface SoccerMatch : NSObject
@property (nonatomic, readonly) NSDate *date; // match date
@property (nonatomic, readonly) NSString *teamDescription; // who's playing
@property (nonatomic, readonly) NSString *groupDescription; // group in the tournament this is
// The model can tell us what the first match in the schedule is and the last match
+ (instancetype)firstMatch;
+ (instancetype)lastMatch;
// and each match can tell us the previous and the next
- (nullable SoccerMatch *)previousMatch;
- (nullable SoccerMatch *)nextMatch;
@end
NS_ASSUME_NONNULL_END
SoccerMatch.m
#import "SoccerMatch.h"
@implementation SoccerMatch
+ (instancetype)firstMatch {
NSCalendar *calendar = [NSCalendar currentCalendar];
// I found out I can create a date using NSDateComponents but Xcode tells me 'date' is unused.
// Apple already created a 'date' in SoccerMatch.h which is used in the ComplicationController so I'm not sure how to fix this.
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:2016];
[components setMonth:3];
[components setDay:21];
[components setHour:10];
[components setMinute:30];
[components setSecond:0];
NSDate *firstMatchDate = [calendar dateFromComponents:components];
SoccerMatch *firstMatchDetails = [[SoccerMatch alloc] init];
firstMatchDetails.date = firstMatchDate;
firstMatchDetails.teamDescription = @"The Bears";
firstMatchDetails.groupDescription = @"Group A";
return firstMatchDetails;
};
+ (instancetype)lastMatch {
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:2016];
[components setMonth:3];
[components setDay:21];
[components setHour:17];
[components setMinute:00];
[components setSecond:0];
NSDate *lastMatchDate = [calendar dateFromComponents:components];
SoccerMatch *lastMatchDetails = [[SoccerMatch alloc] init];
lastMatchDetails.date = lastMatchDate;
lastMatchDetails.teamDescription = @"The Hawks";
lastMatchDetails.groupDescription = @"Group B";
return lastMatchDetails;
};
- (nullable SoccerMatch *)previousMatch {
// Eliza said each match can tell us the previous and the next, but how is that written?
};
- (nullable SoccerMatch *)nextMatch {
};
@end
您的 +firstMatch
和 +lastMatch
方法需要 return SoccerMatch class 的一个实例——这就是 instancetype
关键字的含义。您的日期创建代码看起来不错,但在每个方法中您都需要创建一个 SoccerMatch 对象,用日期和 team/group 描述填写其属性,然后 return 该实例。
我观看了 Creating Complications with ClockKit 著名 Apple 工程师 Eliza Block 在 WWDC 上的演示,并在她进行时对整个项目进行了编码。顺便说一句,很棒的教程。真的很彻底。
她展示了界面,但没有透露她的足球比赛迷你模型的实现。她可能认为这非常容易,所以没有表现出来。然而,作为一名新程序员,我希望看到一些实施示例以及它是如何完成的。
我已经包含了 ComplicationController class 和 SoccerMatch 模型接口。我尝试使用 NSDateComponents 创建日期,但 Xcode 警告我未使用的变量 'date'。此外,日期 @属性 已由 Apple 在界面中创建。实现中要写什么才能得到完整的模型?
import ClockKit
let MatchDuration = NSTimeInterval(60 * 90)
class ComplicationController: NSObject, CLKComplicationDataSource {
// MARK: - Timeline Configuration
func getSupportedTimeTravelDirectionsForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimeTravelDirections) -> Void) {
handler([.Forward, .Backward])
}
func getTimelineStartDateForComplication(complication: CLKComplication, withHandler handler: (NSDate?) -> Void) {
let startDate = timelineEntryDateForMatch(SoccerMatch.firstMatch())
handler(startDate)
}
func getTimelineEndDateForComplication(complication: CLKComplication, withHandler handler: (NSDate?) -> Void) {
let endDate = (SoccerMatch.lastMatch().date.dateByAddingTimeInterval(MatchDuration))
handler(endDate)
}
func getPrivacyBehaviorForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationPrivacyBehavior) -> Void) {
handler(.ShowOnLockScreen)
}
// MARK: - Timeline Population
func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: ((CLKComplicationTimelineEntry?) -> Void)) {
getTimelineEntriesForComplication(complication, beforeDate: NSDate(), limit: 1) { (entries) -> Void in
handler(entries?.first)
}
}
func getTimelineEntriesForComplication(complication: CLKComplication, beforeDate date: NSDate, limit: Int, withHandler handler: (([CLKComplicationTimelineEntry]?) -> Void)) {
var entries = [CLKComplicationTimelineEntry]()
var match : SoccerMatch? = SoccerMatch.lastMatch()
while let thisMatch = match {
let thisEntryDate = timelineEntryDateForMatch(thisMatch)
if date.compare(thisEntryDate) == .OrderedDescending {
let tmpl = templateForMatch(thisMatch)
let entry = CLKComplicationTimelineEntry(date: thisEntryDate, complicationTemplate: tmpl)
entries.append(entry)
if entries.count == limit { break }
}
match = match?.previousMatch()
}
handler(entries)
}
func getTimelineEntriesForComplication(complication: CLKComplication, afterDate date: NSDate, limit: Int, withHandler handler: (([CLKComplicationTimelineEntry]?) -> Void)) {
var entries = [CLKComplicationTimelineEntry]()
var match : SoccerMatch? = SoccerMatch.firstMatch()
while let thisMatch = match {
let thisEntryDate = timelineEntryDateForMatch(thisMatch)
if date.compare(thisEntryDate) == .OrderedAscending {
let tmpl = templateForMatch(thisMatch)
let entry = CLKComplicationTimelineEntry(date: thisEntryDate, complicationTemplate: tmpl)
entries.append(entry)
if entries.count == limit { break }
}
match = match?.nextMatch()
}
handler(entries)
}
// MARK: - Update Scheduling
func getNextRequestedUpdateDateWithHandler(handler: (NSDate?) -> Void) {
handler(nil);
}
// MARK: - Placeholder Templates
func getPlaceholderTemplateForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTemplate?) -> Void) {
let tmpl = CLKComplicationTemplateModularLargeStandardBody()
let soccerBall = UIImage(named: "soccer_ball")!
tmpl.headerImageProvider = CLKImageProvider(onePieceImage: soccerBall)
tmpl.headerTextProvider = CLKSimpleTextProvider(text: "Match schedule")
tmpl.body1TextProvider = CLKSimpleTextProvider(text: "2015 Women's Tournament")
handler(tmpl)
}
private func templateForMatch(match: SoccerMatch) -> CLKComplicationTemplate {
let tmpl = CLKComplicationTemplateModularLargeStandardBody()
let soccerBall = UIImage(named: "soccer_ball")!
tmpl.headerImageProvider = CLKImageProvider(onePieceImage: soccerBall)
tmpl.headerTextProvider = CLKTimeTextProvider(date: match.date)
tmpl.body1TextProvider = CLKSimpleTextProvider(text: match.teamDescription)
tmpl.body2TextProvider = CLKSimpleTextProvider(text: match.groupDescription)
return tmpl
}
private func timelineEntryDateForMatch(match: SoccerMatch) -> NSDate {
if let previousMatch = match.previousMatch() {
return previousMatch.date.dateByAddingTimeInterval(MatchDuration)
} else {
return match.date.dateByAddingTimeInterval(-6 * 60)
}
}
SoccerMatch.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface SoccerMatch : NSObject
@property (nonatomic, readonly) NSDate *date; // match date
@property (nonatomic, readonly) NSString *teamDescription; // who's playing
@property (nonatomic, readonly) NSString *groupDescription; // group in the tournament this is
// The model can tell us what the first match in the schedule is and the last match
+ (instancetype)firstMatch;
+ (instancetype)lastMatch;
// and each match can tell us the previous and the next
- (nullable SoccerMatch *)previousMatch;
- (nullable SoccerMatch *)nextMatch;
@end
NS_ASSUME_NONNULL_END
SoccerMatch.m
#import "SoccerMatch.h"
@implementation SoccerMatch
+ (instancetype)firstMatch {
NSCalendar *calendar = [NSCalendar currentCalendar];
// I found out I can create a date using NSDateComponents but Xcode tells me 'date' is unused.
// Apple already created a 'date' in SoccerMatch.h which is used in the ComplicationController so I'm not sure how to fix this.
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:2016];
[components setMonth:3];
[components setDay:21];
[components setHour:10];
[components setMinute:30];
[components setSecond:0];
NSDate *firstMatchDate = [calendar dateFromComponents:components];
SoccerMatch *firstMatchDetails = [[SoccerMatch alloc] init];
firstMatchDetails.date = firstMatchDate;
firstMatchDetails.teamDescription = @"The Bears";
firstMatchDetails.groupDescription = @"Group A";
return firstMatchDetails;
};
+ (instancetype)lastMatch {
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:2016];
[components setMonth:3];
[components setDay:21];
[components setHour:17];
[components setMinute:00];
[components setSecond:0];
NSDate *lastMatchDate = [calendar dateFromComponents:components];
SoccerMatch *lastMatchDetails = [[SoccerMatch alloc] init];
lastMatchDetails.date = lastMatchDate;
lastMatchDetails.teamDescription = @"The Hawks";
lastMatchDetails.groupDescription = @"Group B";
return lastMatchDetails;
};
- (nullable SoccerMatch *)previousMatch {
// Eliza said each match can tell us the previous and the next, but how is that written?
};
- (nullable SoccerMatch *)nextMatch {
};
@end
您的 +firstMatch
和 +lastMatch
方法需要 return SoccerMatch class 的一个实例——这就是 instancetype
关键字的含义。您的日期创建代码看起来不错,但在每个方法中您都需要创建一个 SoccerMatch 对象,用日期和 team/group 描述填写其属性,然后 return 该实例。