最近我看到了Apple的2015年WWDC主题演讲.我也查看了一些文档,但是我找不到"if if pattern"部分,它是如何写在他们展示的幻灯片上的.(来自Apple Events的 68分00秒视频)
你知道这是指什么吗?还是语法?
一位老C程序员可以使用Swift的一些帮助.
我不了解if-case语法.例如:
if case 20...30 = age {
print ("in range.")
}
Run Code Online (Sandbox Code Playgroud)
在case 20...30 = age似乎是在条件测试if语句.所以我最初很困惑,看到使用了赋值运算符('=')而不是比较运算符('==').
好吧,我心想,这可能意味着该case语句实际上是一个返回布尔值的函数调用.然后,返回的值将满足if语句中的比较测试.
作为一个实验,我尝试将case语句视为常规条件测试,并在其周围放置括号.斯威夫特很乐意接受if (x == 5)或if (true).但是会if (case 20...30 = age)产生错误.因此case声明似乎不像函数那样.
我很想知道这里发生了什么.任何见解将不胜感激.
我有一个整数x,我想检查它是否位于给定范围内的给定边界之间.
直截了当的方法是
let contains = x > lowerBounds && x < higherBounds
Run Code Online (Sandbox Code Playgroud)
对此有一种更加敏捷的方法吗?
我有一本关于人们出生和死亡的字典。
我想知道哪一年活着的人最多。
我目前的算法将每个人生活的每一年都附加到一个数组中,然后返回最高出现次数。我有一种预感,有一种更简洁的方法可以实现这一目标。
这是我临时的蛮力实现:
var people = [ "Nicolas": (birth: 1900, death: 1975),
"Vladimir": (birth: 1970, death: 2000),
"Julius": (birth: 1950, death: 1985),
"Alexander": (birth: 1900, death: 1920),
"Obama": (birth: 1910, death: 1920),
"George": (birth: 1915, death: 1920),
"Benjamin": (birth: 1919, death: 1925)]
var yearsArray = [Int]()
var yearOccurrences: [Int:Int] = [:]
for life in people.values {
var birth = life.birth
var death = life.death
for year in birth..<death {
yearsArray.append(year)
}
}
for year in yearsArray {
yearOccurrences[year] = …Run Code Online (Sandbox Code Playgroud) 我正试图找出另一种方法来做这样的事情,使用范围运算符.
guard let statusCode = (response as? HTTPURLResponse)?.statusCode, statusCode >= 200 && statusCode <= 299 else {return}
Run Code Online (Sandbox Code Playgroud)
也许是这样的:
guard let statusCode = (response as? HTTPURLResponse)?.statusCode where (200...299).contains(statusCode) else {return}
Run Code Online (Sandbox Code Playgroud)
要么
guard let statusCode = (response as? HTTPURLResponse)?.statusCode, statusCode case 200...299 else {return}
Run Code Online (Sandbox Code Playgroud)
这在Swift中可能吗?
我想知道是否有任何平滑的方法来检查swift范围内的整数值.
我有一个整数,可以是0到1000之间的任何数字
我想写一个if语句,"如果整数在300到700之间 - 这样做,如果有任何其他数字 - 做其他事情"
我可以写:
if integer > 300 {
if integer < 700 {
//do something else1
}
//do something
} else {
// do something else2
}
Run Code Online (Sandbox Code Playgroud)
但是我想最小化要编写的代码量,因为"做其他事情1"和"做其他事情2"应该是相同的
你好像不能写:
if 300 < integer < 700 {
} else {
}
Run Code Online (Sandbox Code Playgroud)
我试过用
if integer == 300..<700 {
}
Run Code Online (Sandbox Code Playgroud)
但那也不起作用.有人有个建议吗?
我正在制作一个函数来获取特定工作日的特定日期
前任。2016 年 9 月的星期日 = 4,11,18,25
import UIKit
var daysOff = [Int]()
func getNumberOfDaysInMonth (_ month : Int , _ Year : Int ,lookingFor : Int) -> (firstDay: Int , daysResults: [Int]) {
var day = Int()
if lookingFor > 7 || lookingFor < 1 {
print("error")
return (0,[0])
}else {
day = lookingFor
}
let dateComponents = NSDateComponents()
dateComponents.year = Year
dateComponents.month = month
let calendar = Calendar(identifier: .gregorian)
let date = calendar.date(from: dateComponents as DateComponents)!
let range …Run Code Online (Sandbox Code Playgroud) 我是编程新手。自学所以想向互联网寻求帮助。
我想看看我存储在常量中的 Int 是否与 CountableClosedRange 中的单个数字匹配,如果是,则输出布尔值 true。见下文:
let physics = 76
let math = 79
let history = 94
let language = 98
let aPlus = [95...100]
let didPhysicsGetA = physics == aPlus
Run Code Online (Sandbox Code Playgroud)
76 显然不在这个范围内,所以在这种情况下,我希望 didPhysicsGetA 返回为 false。
我该怎么做,还有更好的方法吗?
我需要在swift中编码'如果数字介于1到5之间'
我尝试了这个,但它不起作用.
if myPercent == (1 ... 5) {
let numberGroup = 1
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
if indexPath.row == 0 ... 11 {
}
Run Code Online (Sandbox Code Playgroud)
那是行不通的,它说二进制运算符'=='不能应用于类型'Int'和'CountableClosedRange的操作数。
(这样做的目的是禁用第0-12行。)
正确的方法是什么?基本问题,但我不知道该搜索什么。提前致谢!
如果我不得不猜测将是:
for numero in 0 ... 11 {
if indexPath.row == numero {
}
}
Run Code Online (Sandbox Code Playgroud)