-2

Ich sah die Creating Complications with ClockKit Präsentation bekannte Apple-Ingenieur Eliza Block gab auf der WWDC und codierte das ganze Projekt, als sie mitging. Tolles Tutorial übrigens. Wirklich gründlich.Wie würde ich ein Modell für diese WatchKit-Komplikationen erstellen?

Sie zeigte die Schnittstelle, enthüllte aber nicht die Implementierung des Mini-Modells für ihre Fußballspiele. Sie dachte wahrscheinlich, dass es super einfach war, also zeigte es einfach nicht. Als neuer Programmierer möchte ich jedoch einige Beispiele für die Implementierung und deren Durchführung sehen.

Ich habe die ComplicationController-Klasse und die SoccerMatch-Modellschnittstelle eingefügt. Ich habe versucht, ein Datum mit NSDateComponents zu erstellen, aber Xcode warnt mich vor der unbenutzten Variable 'date'. Auch das Datum @property wurde bereits von Apple in der Oberfläche erstellt. Was sollte ich in der Implementierung schreiben, um ein komplettes Modell zu erhalten?

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 
+0

Ich fand heraus, dass ich ein Datum mit 'NSDateComponents' erstellen kann, aber Xcode sagt mir, dass 'date' nicht verwendet wird. Apple hat bereits eine "Datum" -Eigenschaft in SoccerMatch.h erstellt, die im ComplicationController verwendet wird, so dass ich mir nicht sicher bin, wie ich das beheben kann. – TokyoToo

Antwort

0

Ihre +firstMatch und +lastMatch Methoden benötigen eine Instanz der Klasse SoccerMatch-dass zurückzukehren ist, was das instancetype Schlüsselwort bedeutet. Ihr Datumserstellungscode sieht gut aus, aber füllen Sie bei jeder Methode, die Sie zum Erstellen eines SoccerMatch-Objekts benötigen, seine Eigenschaften mit dem Datum und den Team-/Gruppenbeschreibungen aus, und geben Sie dann diese Instanz zurück.

+0

Danke Dr. Zeus. Ich bin super-noob hier, also könnten Sie bitte meinen aktualisierten Code überprüfen. Ich habe das Objekt erstellt und versucht, den Eigenschaften Werte zuzuweisen, erhalte jedoch den Fehler 'Zuordnung zu schreibgeschütztem Merkmal'. Apple hat sie als schreibgeschützt geschrieben, also möchte ich sie so behalten, weil ich ihren Code so testen möchte, wie er ihn präsentiert hat. Welchen Ausdruck soll ich auch in die zwei letzten Methoden previousMatch & nextMatch setzen? Danke – TokyoToo