mac快速切换大小写_快速模式匹配–如果是大小写,请切换为大小写

article/2025/10/27 13:17:32

mac快速切换大小写

In this tutorial, we’ll look into Pattern Matching in Swift. Pattern matching is seen in switch statements.

在本教程中,我们将研究Swift中的模式匹配。 在switch语句中可以看到模式匹配。

快速模式匹配 (Swift Pattern Matching)

The easy to use switch syntax of Swift can be extended to for and if statements as well.
Pattern matching is used to match tuples, arrays, enums etc. or a part of them.

Swift的易于使用的开关语法也可以扩展为for和if语句。
模式匹配用于匹配元组,数组,枚举等或其一部分。

Here’s a very basic example of Pattern matching:

这是一个非常基本的模式匹配示例:

for _ in 1...10{
}

Let’s fire up our XCode Playground and start Swifting.

让我们启动XCode Playground并开始Swifting。

部分匹配元组 (Partial Matching Tuples)

In Switch statements, partial matching is very commonly used and implemented as shown below:

在Switch语句中,部分匹配非常普遍地使用和实现,如下所示:

let author1 = ("Anupam","Android")
let author2 = ("Pankaj","Java")
let author3 = ("Anupam","Swift")
let author4 = ("Anupam","Java")func switchCasePatternTuple(tuple : (String,String))
{switch tuple {case (let name, "Java"):print("\(name) writes on Java")case let(_,category):print("Some author writes on \(category)")}
}switchCasePatternTuple(tuple: author1)
switchCasePatternTuple(tuple: author2)
switchCasePatternTuple(tuple: author3)
switchCasePatternTuple(tuple: author4)

In the above code, we are partially matching only one of the values in the tuple.
Following is the output:

在上面的代码中,我们仅部分匹配元组中的值之一。
以下是输出:

Swift Pattern Matching Tuple Partial Output

Swift Pattern Matching Tuple Partial Output

Swift模式匹配元组部分输出

We can write let to each parameter name or like case let as we did in the above snippet.
let is used to bind associated values.

我们可以像上面片段中那样将let写入每个参数名称或类似大小写的let。
let用于绑定关联值。

配套选配 (Matching Optionals)

Swift has a way of matching optionals.
We can do that by using the .some and .none syntax or just setting a ? on the parameter.

Swift有一种匹配可选选项的方法。
我们可以通过使用.some.none语法或仅设置一个?来做到这一点。 在参数上。

let y : Int? = 1
if case .some = y{print("Value of optional is \(y)")
}let x : Int? = 5
if case .some = x{print("Value of optional is \(x)")
}if case .none = x
{print("Optional x is nil")
}if case let z? = x, z>0{print("Value of implicitly unwrapped optional is \(z)")
}

if case let x = y is equal to switch y {case let x: }

if case let x = y等于switch y {case let x: }

The output printed in the console is:

控制台中输出的输出为:

Value of optional is Optional(1)
Value of optional is Optional(5)
Value of implicitly unwrapped optional is 5

Let’s look at a switch case example for optional matching:

让我们看一下可选匹配的开关案例示例:

let name : String? = "Anupam"
let password : String? = "ABCD"let userInfo = (name, password)switch userInfo {
case let (.some(name), .some(password)):print("\(name) password is \(password)")
case let (.some(name), nil):print("\(name) does not remember the password.")
case (.none, .some(_)):print("There is some password but no name.")
case (.none, _):print("No user name. No password. ")
}//prints Anupam password is ABCD

Note: if name and password both are nil, the fourth case would be executed.

注意:如果名称和密码均为零,则将执行第四种情况。

Also, instead of some and none, we can use the following syntax as well:

另外,我们也可以使用以下语法来代替某些语法:

switch userInfo {
case let (name?, password?):print("\(name) password is \(password)")
case let (name?, nil):print("\(name) does not remember the password.")
case (.none, _?):print("There is some password but no name.")
case (.none, _):print("No user name. No password. ")
}

Let’s look at the for case to match optionals.

让我们看一下for情况,以匹配可选项目。

let topics =  ["Java","Android","Python","Django","JS",nil]for case let .some(t) in topics
{print("Topic is \(t)")
}for case let .some(t) in topics where t.starts(with: "J")
{print("Topic starting with J is \(t)")
}
Swift Pattern Matching Optionals Output

Swift Pattern Matching Optionals Output

Swift模式匹配可选输出

where clause is used to set a pattern matching on the optional case.
where子句用于在可选情况下设置模式匹配。

匹配类型 (Matching Types)

We can match types in the switch statements as shown below:

我们可以在switch语句中匹配类型,如下所示:

var randomArray: [Any] = ["Swift", 2, 7.5]
for randomItem in randomArray {switch randomItem {case is Int:print("Int value. I don't care about the value")case is String:print("String type.")default: break}
}

〜=运算符 ( ~= operator)

We can use the ~= operator in the if statements to match a range as shown below:

我们可以在if语句中使用〜=运算符来匹配范围,如下所示:

let age = 25if 0..<13 ~= age{print("Hey kid!!")
}
else if 13...19 ~= age{
print("Hey teenager!!")
}
else if 19...45 ~= age{
print("Hey adult!!")
}//prints Hey adult!!

The ~= operator can be replaced by = or .contains(age) as well but that would cause readability issues.

〜=运算符也可以用=或.contains(age)代替,但这会引起可读性问题。

模式与枚举匹配 (Pattern Matching With Enums)

The last example deals with Pattern matching using Swift Enums.

最后一个示例使用Swift Enums处理模式匹配。

enum Month{case Jan(zodiac: String,gender:String)case Febcase March(zodiac: String)
}let month1 = Month.March(zodiac: "Pisces")
let month2 = Month.March(zodiac: "Aries")
let month3 = Month.Jan(zodiac: "Aqarius", gender: "Female")func switchEnum(month: Month)
{switch month {case .Feb:print("Feb it is")case let .March(zodiac) where zodiac == "Pisces":print("March Pisces. Aries won't fall here.")case let .Jan(_,gender):print("Jan does not care about zodiac. Only shows gender \(gender)")default:print("Others")}
}switchEnum(month: month1)
switchEnum(month: month2)
switchEnum(month: month3)//prints:
//March Pisces. Aries won't fall here.
//Others
//Jan does not care about zodiac. Only shows gender Female

This brings an end to this tutorial on Pattern Matching in Swift.

这样就结束了本本Swift模式匹配教程。

翻译自: https://www.journaldev.com/28894/swift-pattern-matching-if-case-for-case-switch-case

mac快速切换大小写


http://chatgpt.dhexx.cn/article/c7vxjtUG.shtml

相关文章

2021 Mac系统升级后,按大小写键没反应了,切换大小写的灯不亮了

今天把Mac系统升级了&#xff0c;升级后发现caps lock 锁定大小写的键&#xff0c;失灵了&#xff0c;居然可以用来切换输入法了&#xff0c;经过一排查后&#xff0c; 使用以下几种方法处理&#xff1a; 方式一&#xff1a;长按 caps lock 键&#xff0c;来切换大小写 方式…

mac大小写切换快捷键,程序猿向

以前一直是在command&#xff0b;空格切换中文输入法加 英文输入法用的。很麻烦啊&#xff0c;写代码一只要切啊切。。。 原来按下caps lock 就可以输入小写英文了&#xff0c;再按住shift就是大写英文。松掉继续小写 这样不需要两个键&#xff0c;只需要一个键&#xff0c;就可…

Captin for mac(大小写切换悬浮窗提示)

Captin for ma是一款运行在MacOS上的大小写切换悬浮窗提示工具。Captin中文版体积小巧&#xff0c;停驻在菜单栏上&#xff0c;可以即时的显示大小写的状态并通过声音或图像展示给你&#xff0c;你还可以自定义它的LED颜色。使用简单方便。 测试环境&#xff1a;MacOS 10.14.6…

Mac 打开大小写切换很慢

发现出现这个问题的人还不少&#xff0c;记录一下。 大小写切换很慢的问题&#xff0c;遇到真的很头疼&#xff0c;想砸电脑。 处理方法&#xff1a; 系统偏好设置>键盘>输入法>取消勾选【使用大小写锁定键盘切换"ABC".....】 当看不到该选项时&#xff0…

Mac大小写切换需长按capslock键解决办法

从windows系统过渡到mac系统&#xff0c;由于两者之间的键盘输入存在诸多不一致的地方&#xff0c;为了提高工作效率&#xff0c;我们习惯使用windows的大小写切换方式&#xff0c;由于mac默认大小写切换选择 长按切换&#xff0c;因此只需要取出该选项即可。 具体操作如下&am…

苹果Mac怎样切换大写输入法?

很多用惯了windows系统的小伙伴们刚开始用macbook是很不习惯&#xff0c;打字时需要大小写切换。Mac输入法有些不同&#xff0c;那么mac系统大写输入法怎么打开&#xff1f;大家可以自己定义键盘来进行大小写切换&#xff0c;跟着macz小编一起来看看具体操作。 具体方法如下&a…

正交基的性质

矩阵分块计算 A [ b 1 ⋯ b p ] [ A b 1 ⋯ A b p ] A\left[\boldsymbol{b}_{1} \cdots \boldsymbol{b}_{p}\right]\left[A \boldsymbol{b}_{1} \cdots A \boldsymbol{b}_{p}\right] A[b1​⋯bp​][Ab1​⋯Abp​] [ ∣ ∣ a 1 ⋯ a n ∣ ∣ ] [ − b 1 − ⋮ − b n − ] [ …

线性代数——正交矩阵

正交矩阵 orthogonal matrix 正交矩阵的定义正交矩阵性质1)AT是正交矩阵2)A的各行是单位向量且两两正交3)A的各列是单位向量且两两正交4)|A|1或-1 正交矩阵的定义 如果&#xff1a;AATE&#xff08;E为单位矩阵&#xff0c;AT表示“矩阵A的转置矩阵”。&#xff09;或ATAE&…

线性代数(六)正交性

文章目录 一&#xff1a;内积、长度、正交性1.1内积1.2长度1.3正交向量1.4总结 二&#xff1a;正交集2.1定义2.2定理--正交基2.3正交投影2.4单位正交集 三&#xff1a;正交矩阵3.1单位正交列向量3.2性质3.3正交矩阵初入门 四&#xff1a;拉格姆-施密特方法4.1定义4.2步骤4.3例子…

施密特正交化(Gram-Schmidt Orthogonalization)

目录 1 Gram-Schmidt的计算公式推导2 Gram-Schmidt的意义3 Modified Gram-Schmidt (以算法模式计算正交向量)3.1 Modified G-S会出现的问题&#xff1a;当矩阵开始存在微小误差时&#xff0c;会在运算过程中不断累积误差&#xff0c;导致越算越不准确&#xff0c;以至于计算所得…

线性代数(9):线性正交

一、正交向量组 &#xff08;1&#xff09;定义 若一个非零向量组中的向量两两相交&#xff0c;则称该向量组为正交向量组&#xff1b; 由单个非零向量组成的向量组也为正交向量组 &#xff08;2&#xff09;判断 1.2.1 方法 证明两两相交的的方法就是计算向量的内积和是否为…

两个图片叠加在一起css,css实现图片叠加的几种思路(记录笔记)

3、使用div层叠&#xff0c;设置两个div&#xff0c;一个position:relative;一个position:absolute; 就可以实现在div里面显示绝对定位&#xff0c;而不跑出div的层叠div&#xff0c;对于没有固定整个界面的width和height的情况适用。 如果界面的宽高都固定了像素值&#xff0c…

学习如何使用html和css样式将两张图片叠加到另一张图片上,实现微信扫一扫二维码效果

学习如何使用html和css样式将两张图片叠加到另一张图片上&#xff0c;实现微信扫一扫二维码效果 <!DOCTYPE html> <html> <head><meta charset"utf-8"/><link rel"stylesheet" href"menuStyle.css"/><title>…

Matlab中实现两张图片的叠加显示效果

Matlab中实现两张图片的叠加显示效果 1、相同大小图片的叠加显示2、不同大小图片的叠加显示 ** 在matlab中以50%透明度实现两张图图片的叠加显示&#xff0c;图片的大小可以任意设置&#xff0c;不同大小的图片&#xff0c;较小的图片在整幅图中居中显示。** 1、相同大小图片的…

java中将两个图片进行叠加

有个需求要在一张图片上绘制图案&#xff0c;不好实现&#xff0c;就想到把图案的图片直接叠加到背景图片上&#xff0c;试了一下&#xff0c;居然成功了&#xff0c;话不多说&#xff0c;直接看代码&#xff1a; public static void main(String[] args) {try {BufferedImage…

android 中关于两张图片叠加方法(记录)

最近在做一个小的Android项目中遇到一个问题&#xff0c;就是不知道为什么机器输出的分辨率不稳定&#xff0c;总是有几十个像素的误差。导致屏幕适配出现了问题。这次主要记录一下解决思路。 问题就如图 主要是一张背景图 &#xff0c;在背景图指定区域去镶嵌一张指定图片。…

html盒子两个背景图片,css怎么实现两张图片叠加在一起,css添加盒子背景图片

css怎么实现两张图片叠加在一起CSS怎么实现了两张图片的叠加&#xff0c;Css实现了两张图片叠加在一起的方法&#xff1a;可以通过分别设置div与页面左边缘的距离和div与页面上边缘的距离来实现。需要注意的是&#xff0c;两张图片都应该设置position:absolute属性。 环境&…

两个图片叠加在一起css,css两张图片怎么叠加在一起?

css实现两张图片叠加在一起的方法&#xff1a;首先添加2个img标签&#xff1b;然后设置它们的css样式为position:absolute&#xff1b;最后设置其中一个img样式为left:120px即可看见效果。 使用css把两个图片叠加&#xff0c;可以通过position定位属性设置两张图片的位置来实现…

使用Vue将两张图片叠加再保存为一张图片下载

最终效果 将一张课程图片和一张二维码图片叠加&#xff08;网上图片随便乱找&#xff0c;勿对号入座&#xff01;&#xff01;&#xff01;&#xff09; 步骤 先将两张图片使用css进行叠加&#xff0c;然后按照自己需求将图片移动到合理位置要使用到一个插件将两张图片转为…

Unity-两张图片叠加合成一张图片

在做图片分享时&#xff0c;往往需要对图片加上水印或其他标签图片&#xff0c;这个时候就要考虑如何对多张图片进行叠加合成为一张图片。 其实一张图片就是一组单色像素组成的像素矩阵 要对两张图片进行叠加&#xff0c;只需要将背景图片中对应的像素颜色&#xff0c;替换成…