gpt4 book ai didi

objective-c - 我如何为这些 WatchKit 并发症创建模型?

转载 作者:行者123 更新时间:2023-11-30 13:35:18 25 4
gpt4 key购买 nike

我观看了Creating Complications with ClockKit著名的苹果工程师 Eliza Block 在 WWDC 上做了演示,并在演示过程中编写了整个项目的代码。顺便说一句,很棒的教程。真的很彻底。

她展示了界面,但没有透露她的足球比赛迷你模型的实现。她可能认为这非常简单,所以只是没有表现出来。然而,作为一名新程序员,我希望看到一些实现的示例以及它是如何完成的。

我已经包含了 ComplicationController 类和 SoccerMatch 模型接口(interface)。我尝试使用 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)
}

}

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

关于objective-c - 我如何为这些 WatchKit 并发症创建模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36139206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com