gpt4 book ai didi

swift nsobject 类如何返回值

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

我是 swift 新手,如何创建公共(public)类并返回字符串这是我的代码,我犯了错误,请帮助我。这是我的 nsobject 公共(public)类

import UIKit

class DayCount: NSObject {

func doSomething(getDate:String) -> String {


let start = "2015­06­01"

let end = NSDate()

let dateFormatter = NSDateFormatter()

dateFormatter.dateFormat = "yyyy­MM­dd"

var endDateSting:String = dateFormatter.stringFromDate(end)

let startDate:NSDate = dateFormatter.dateFromString(start)!

let endDate:NSDate = dateFormatter.dateFromString(endDateSting)!

let cal = NSCalendar.currentCalendar()

let unit:NSCalendarUnit = .CalendarUnitDay

let components = cal.components(unit, fromDate: startDate, toDate:

endDate, options: nil)

println(components)
let date:String? = (self.timeAgoSinceDate(startDate, numericDates: Bool()))

println(date)

return date!


}

func timeAgoSinceDate(date:NSDate, numericDates:Bool) -> String {
let calendar = NSCalendar.currentCalendar()

let unitFlags = NSCalendarUnit.CalendarUnitMinute |

NSCalendarUnit.CalendarUnitHour | NSCalendarUnit.CalendarUnitDay |

NSCalendarUnit.CalendarUnitWeekOfYear

NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitYear |

NSCalendarUnit.CalendarUnitSecond

let now = NSDate()

let earliest = now.earlierDate(date)

let latest = (earliest == now) ? date : now

let components:NSDateComponents = calendar.components(unitFlags,

fromDate: earliest, toDate: latest, options: nil)

if (components.year >= 2) {

} else if (components.year >= 1){

} else if (components.month >= 2) {

} else if (components.month >= 1){

} else if (components.weekOfYear >= 2) {

} else if (components.weekOfYear >= 1){

} else if (components.day >= 2) {

} else if (components.day >= 1){

} else if (components.hour >= 2) {

} else if (components.hour >= 1){

return "\(components.year) years ago"

if (numericDates){

return "1 year ago"

} else {

return "Last year"

}

return "\(components.month) months ago"

if (numericDates){

return "1 month ago"

} else {

return "Last month"

}

return "\(components.weekOfYear) weeks ago"

if (numericDates){

return "1 week ago"

} else {

return "Last week"

}

return "\(components.day) days ago"

if (numericDates){

return "1 day ago"

} else {

return "Yesterday"

}

return "\(components.hour) hours ago"

if (numericDates){

return "1 hour ago"

} else {

return "An hour ago"

}

} else if (components.minute >= 2) {

return "\(components.minute) minutes ago"

} else if (components.minute >= 1){

if (numericDates){

return "1 minute ago"

} else {

return "A minute ago"

}

} else if (components.second >= 3) {

return "\(components.second) seconds ago"

} else {

return "Just now"

}
return ""
}





}

我的 View Controller 得到了这样的字符串,但它的返回总是错误

let getdate = (DayCount.doSomething("sdfsdfsd")) 
println("date===== \(getdate)")

错误是“(字符串)-> 字符串无法转换为 DayCount”我也尝试过这个

let getdate:String = (DayCount.doSomething("sdfsdfsd"))
println("date===== \(getdate)")

但它总是返回错误请帮助我哪里出错了

最佳答案

let getdate = (DayCount.doSomething("sdfsdfsd")) 

不起作用,因为它的语法不正确。doSomething 是一个方法,DayCount 是一个类。在类的实例变量上调用方法。例如

var aDayCounter : DayCount

是定义 Daycount 类型变量的一种方法。然后您可以在该类型上调用该方法

aDayCounter.doSomething("sdfsdfsd")

您还可以定义类方法或类型方法,因为它们在 Swift 中被调用。 Apple 开发网站对此的解释比我更好:

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Methods.html

关于swift nsobject 类如何返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33033193/

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