gpt4 book ai didi

ios - 为什么这段 Swift 代码会出错?

转载 作者:行者123 更新时间:2023-11-28 13:00:59 25 4
gpt4 key购买 nike

我目前正在处理第一个 Swift 项目,并犯了新手的错误。

我正在处理的类旨在将其三个方法导出到全局变量:

var getAngle:[AnyObject]!;
var getHours:[AnyObject]!
var getMinutes:[AnyObject]!;

class GpsViewController : UIViewController {

// ....

required init() {
// super.init();
formatter.dateFormat = "yyyy-MM-dd";
parser.locale = NSLocale(localeIdentifier: "en_US_POSIX");
parser.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ";
getAngle = self.restrictToAngle;
getHours = self.restrictToHours;
getMinutes = self.restrictToMinutes;
}

func getHourAndAngle() { // Hour hand angle in radians from 12 o'clock position, clockwise.
var now = NSDate();
var today = now;
var todayFormatted = this.formatter(today);
var yesterday = today.dateWithTimeIntervalSinceNow(-24 * 60 * 60);
var yesterdayFormatted = this.formatter(yesterday);
var tomorrow = today.dateWithTimeIntervalSinceNow(24 * 60 * 60);
var tomorrowFormatted = this.formatter(tomorrow);
var yesterdaySunset = this.presentLocation[yesterdayFormatted]["sunset"];
var todaySunrise = this.presentLocation[todayFormatted]["sunrise"];
var todaySunset = this.presentLocation[todayFormatted]["sunset"];
var tomorrowSunrise = this.presentLocation[tomorrowFormatted]["sunrise"];

var duration = 0.0;
var position = 0.0;
var startingHour = 18;
var offset = 0.0;
if now.isLessThanDate(todaySunrise) {
length = todaySunrise.timeIntervalSinceDate(yesterdaySunset);
position = now.timeIntervalSinceDate(yesterdaySunset);
offset = -0.5;
} else if now.isLessThanDate(todaySunset) {
length = todaySunset.timeIntervalSinceDate(todaySunrise);
position = now.timeIntervalSinceDate(todaySunrise);
offset = 0.5;
startingHour = 6;
} else {
length = tomorrowSunrise.timeIntervalSinceDate(todaySunset);
position = now.timeIntervalSinceDate(todaySunset);
offset = 1.5;
}
var proportion = position / length;
var angle = M_PI + (2 * M_PI * (proportion + offset));
var hours = floor(24 * 60 * 60 * ((1 + proportion + offset) % 1.0) / (60 * 60));
var minutes = floor(24 * 60 * 60 * (1 + proportion + offset) % 60 * 60);
return ["angle": angle, "hour": hour, "minutes": minutes];
}

func restrictToHours() {
return getHoursMinutesAngle["hour"];
}

func restrictToAngle() {
return getHoursMinutesAngle["angle"];
}

func restrictToMinutes() {
return getHoursMinutesAngle["minutes"];
}

// ...
}

我遇到了几个错误,包括在 init() 的 getAngle 赋值中,“GpsViewController 类型的值没有成员 restrictToAngle”。

你能告诉我这里的 n00b 错误是什么吗?

最佳答案

您正在尝试将函数 self.restrictToAngle 分配给 getAngle,这是一个 [AnyObject]! 类型的变量(即数组) .

关于ios - 为什么这段 Swift 代码会出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33845123/

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