我是swift的新手如何创建公共类和返回字符串这是我的代码我错了请帮助我。这是我的nsobject普通类
import UIKit
class DayCount: NSObject {
func doSomething(getDate:String) -> String {
let start = "20150601"
let end = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"
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 ""
}
}
和我的视图控制器我得到这样的字符串,但它的返回总是错误
let getdate = (DayCount.doSomething("sdfsdfsd"))
println("date===== \(getdate)")
错误是“(字符串) - >字符串无法转换为DayCount” 我也试试这个
let getdate:String = (DayCount.doSomething("sdfsdfsd"))
println("date===== \(getdate)")
但是它总是回复我的错误请帮助我,我在哪里做错了
答案 0 :(得分:1)
let getdate = (DayCount.doSomething("sdfsdfsd"))
因为语法不正确而无法工作。 doSomething是一种方法,DayCount是一个类。在类的实例变量上调用方法。 例如
var aDayCounter : DayCount
是一种定义Daycount类型变量的方法。在那种类型上,您可以调用方法
aDayCounter.doSomething("sdfsdfsd")
您还可以在Swift中调用Class方法或Type方法。 Apple开发网站对此的解释比我能做的更好: