简体   繁体   English

如何创建 function 以从 Swift 的年份返回世纪?

[英]How can I create a function to return the century from the year in Swift?

I have a problem in code fights.我在代码斗争中遇到了问题。 I am getting an error:我收到一个错误:

declaration is only valid at file scope (extension Decimal)声明仅在文件 scope 中有效(扩展十进制)

Can someone please guide me on how to fix this?有人可以指导我如何解决这个问题吗? BTW I am creating a function to return the century with the year as an input.顺便说一句,我正在创建一个 function 来以年份作为输入返回世纪。 If you have any recommendations for my code let me know.如果您对我的代码有任何建议,请告诉我。

func centuryFromYear(year: Int) -> Int {
    let centuryOtherStart = year / 100
    let centuryStart = Double(year / 100)
    let centuryEnd = round(centuryStart)
    var wholeNumber : Bool
    if wholeNumber == true {
        return Int(centuryStart)

    } else {
        return Int(centuryEnd + 1)
    }

    extension Decimal {
        var isWholeNumber: Bool {
            wholeNumber = self.isZero || (self.isNormal && self.exponent >= 0)
        }
    }
}

Solution is very simple:解决方法很简单:

func centuryFromYear(year: Int) -> Int {    
    return (year + 99) / 100
}

Beside the fact that you can't declare an extension inside a function, you need to extend FloatingPoint instead of Decimal .除了不能在函数内声明扩展这一事实之外,您还需要扩展FloatingPoint而不是Decimal Just add a new Swift file to your project and add the extension there:只需在您的项目中添加一个新的 Swift 文件并在那里添加扩展名:

extension FloatingPoint {
    var isWholeNumber: Bool {
        return isZero ? true : isNormal ? self == rounded() : false
    }
}

Regarding in your method to extract the year century there are a few mistakes.关于您提取年份世纪的方法有一些错误。 First you should only divide by 100 after converting your year to Double.首先,在将年份转换为 Double 后,您应该只除以 100。 Second you need to return the rounded if the result is whole number otherwise return it without rounding and incrementing one:其次,如果结果是整数,则需要返回四舍五入,否则返回它而不进行四舍五入和递增一:

func centuryFrom(year: Int) -> Int {
    let centuryStart = Double(year)/100
    return centuryStart.isWholeNumber ? Int(round(centuryStart)) : Int(centuryStart) + 1
}

Testing:测试:

centuryFrom(year: 1801)   // XIX
centuryFrom(year: 1900)   // XIX
centuryFrom(year: 1901)   // XX
centuryFrom(year: 2000)   // XX
centuryFrom(year: 2001)   // XXI
centuryFrom(year: 2100)   // XXI
centuryFrom(year: 2101)   // XXII

I might be late.我可能会迟到。 But try this one too但也试试这个

    func centuryFromYear(year: Int) -> Int {
        if year % 100 == 0 {
//this will return exact century like 2000 means 20th Century
            return year / 100 
        }else {
//this will return exact century like 2001 means 20th Century

            return year / 100 + 1   
        }
    }

There's a couple of ways,有几种方法,

you can do this你可以这样做

Op1操作1

func century(_ year: Double) -> String {
    var century = Int((year / 100).rounded(.up))
    if century != 21 {
    return "\(century)th century"
    } else {
    return "21st century"
    }
}

or Op2Op2

func century(_ year: Int) -> String {
    switch year {
    case 901...1000: return "10th century"
    case 1001...1100: return "11th century"
    case 1101...1200: return "12th century"
    case 1201...1300: return "13th century"
    case 1301...1400: return "14th century"
    case 1401...1500: return "15th century"
    case 1501...1600: return "16th century"
    case 1601...1700: return "17th century"
    case 1701...1800: return "18th century"
    case 1801...1900: return "19th century"
    case 1901...2000: return "20th century"
    case 2001...2100: return "21st century"
    default: return ""
    }
}
func centuryFromYear(year: Int) -> Int {
    return 1 + Int((year - 1) / 100)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建嵌套的 Encodable class 并从 Swift 中的 function 返回它? - How to create a nested Encodable class and return it from a function in Swift? 无法调用非函数类型'JSON'的值,如何从Swift中的块返回值 - Cannot call value of non-function type 'JSON', How can I return value from block in Swift 在XCODE / Swift中,如何创建可以从各种视图控制器调用的单个函数? - In XCODE / Swift, how can I create a single function which can be called from various view controllers? 如何在 Swift 中的函数中的 return 语句之后运行代码? - How can I run code after the return statement in a function in Swift? 如何在 Swift 中构建递归 function 以返回字符串? - How can I build a recursive function in Swift to return a String? 如何使用Swift在此Alamofire函数中返回true / false? - How can I return true/false in this Alamofire function using Swift? 如何从swift 3中的@IBAction函数返回一个Int变量? - How do I return an Int variable from an @IBAction function in swift 3? 我正在尝试使用 Swift 在我的 iOS 应用程序中创建一个日历,但我无法从 Date() 中获取日期、工作日或年份值 - i'm trying to create a calendar in my iOS app with Swift but i can't grab day, weekday, or year values from Date() 如何在Swift中创建类似于Ruby中的扫描的函数 - How can I create a function in Swift that is similar to scan in Ruby 我如何从这个 function 返回值? - how can i return value from this function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM