1

我观看了著名的 Apple 工程师 Eliza Block 在 WWDC 上演示的用 ClockKit 创建并发症,并在她进行过程中对整个项目进行了编码。很棒的教程顺便说一句。真是彻底。

她展示了界面,但没有透露用于她的足球比赛的迷你模型的实现。她可能认为这非常容易,所以没有表现出来。然而,作为一个新程序员,我想看看一些实现的例子以及它是如何完成的。

我已经包含了 ComplicationController 类和 SoccerMatch 模型接口。我尝试使用 NSDateComponents 创建日期,但 Xcode 警告我未使用变量“日期”。此外,Apple 已经在界面中创建了日期 @property。我应该在实现中写什么以获得完整的模型?

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)
    }

}

足球比赛.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

足球比赛.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
4

1 回答 1

0

你的+firstMatch+lastMatch方法需要返回一个 SoccerMatch 类的实例——这就是instancetype关键字的含义。您的日期创建代码看起来不错,但在每个方法中,您需要创建一个 SoccerMatch 对象,用日期和团队/组描述填写其属性,然后返回该实例。

于 2016-03-21T23:49:20.587 回答