Swift
文章平均质量分 53
Swift相关的笔记、知识等
豪冷啊
@Haomissyou,iOS开发工程师,个人作品:小五笔,小笔记,小汉字,小挑战,小编辑器等等
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Swift 手动导入 RxSwift.xcframework 报错
手动修复签名,能 Build 成功!原创 2025-03-12 19:08:36 · 490 阅读 · 1 评论 -
【Swift 60秒】97 - Optionals: Summary
You've made it to the end of the tenth part of this series, so let's summarize:原创 2023-02-23 10:17:19 · 503 阅读 · 0 评论 -
【Swift 60秒】96 - Typecasting
Swift must always know the type of each of your variables, but sometimes you know more information than Swift does. For example, here are three classes:原创 2023-02-21 10:07:51 · 503 阅读 · 0 评论 -
【Swift 60秒】95 - Failable initializers
When talking about force unwrapping, I used this code:原创 2023-02-17 10:40:34 · 573 阅读 · 0 评论 -
【Swift 60秒】94 - Optional try
Back when we were talking about throwing functions, we looked at this code:原创 2023-02-15 10:29:10 · 564 阅读 · 0 评论 -
【Swift 60秒】93 - Optional chaining
Swift provides us with a shortcut when using optionals: if you want to access something like `a.b.c` and `b` is optional, you can write a question mark after it to enable optional chaining: `a.b?.c`.原创 2023-02-14 16:44:47 · 545 阅读 · 0 评论 -
【Swift 60秒】92 - Nil coalescing
The `nil coalescing` operator unwraps an optional and returns the value inside if there is one. If there *isn't* a value - if the optional was `nil` - then a default value is used instead. Either way, the result won't be optional: it will either by the val原创 2023-02-13 09:30:56 · 543 阅读 · 0 评论 -
【Swift 60秒】91 - Implicitly unwrapped optionals
Like regular optionals, implicitly unwrapped optionals might contain a value or they might be nil. However, unlike regular optionals you don't need to unwrap them in order to use them: you can use them as if they weren't optional at all.原创 2023-02-10 11:08:27 · 481 阅读 · 0 评论 -
【Swift 60秒】90 - Force unwrapping
Optionals represent data that may or may not be there, but sometimes you know for **sure** that a value isn't nil. In these cases, Swift lets you force unwrap the optional: convert it from an optional type to a non-optional type.原创 2023-02-08 11:06:59 · 638 阅读 · 0 评论 -
【Swift 60秒】89 - Unwrapping with guard
An alternative to if let is guard let, which also unwraps optionals. guard let will unwrap an optional for you, but if it finds nil inside it expects you to exit the function, loop, or condition you used it in.原创 2023-02-07 10:00:46 · 397 阅读 · 0 评论 -
【Swift 60秒】88 - Unwrapping optionals
Optional strings might `contain` a string like "Hello" or they might be `nil` - nothing at all.原创 2023-02-06 10:27:06 · 710 阅读 · 0 评论 -
【Swift 60秒】87 - Handling missing data
We've used types such as `Int` to hold values like 5. But if you wanted to store an `age` property for users, what would you do if you didn't know someone's age?原创 2023-02-03 10:24:53 · 414 阅读 · 0 评论 -
【Swift 60秒】86 - Protocols and extensions: Summary
You've made it to the end of the ninth part of this series, so let's summarize:原创 2023-02-02 09:46:47 · 425 阅读 · 0 评论 -
【Swift 60秒】85 - Protocol-oriented programming
`Protocol extensions` can provide default implementations for our own protocol methods. This makes it easy for types to conform to a protocol, and allows a technique called "protocol-oriented programming" - crafting your code around protocols and protocol原创 2023-02-01 10:34:44 · 783 阅读 · 0 评论 -
【Swift 60秒】84 - Protocol extensions
`Protocols` let you describe what methods something should have, but don't provide the code inside. `Extensions` let you provide the code inside your methods, but only affect one data type - you can't add the method to lots of types at the same time.原创 2023-01-31 10:27:44 · 507 阅读 · 0 评论 -
【Swift 60秒】83 - Extensions
Extensions allow you to add methods to existing types, to make them do things they weren't originally designed to do.原创 2023-01-30 10:06:03 · 301 阅读 · 0 评论 -
【Swift 60秒】82 - Protocol inheritance
One protocol can `inherit` from another in a process known as protocol inheritance. Unlike with classes, you can inherit from `multiple` protocols at the same time before you add your own customizations on top.原创 2023-01-16 10:19:02 · 565 阅读 · 0 评论 -
【Swift 60秒】81 - Protocols
`Protocols` are a way of describing what properties and methods something must have.You then tell Swift which types use that protocol - a process known as adopting or conforming to a protocol.原创 2023-01-13 09:41:56 · 621 阅读 · 0 评论 -
【Swift 60秒】80 - Classes: Summary
You’ve made it to the end of the eighth part of this series, so let’s summarize原创 2023-01-11 09:50:19 · 344 阅读 · 0 评论 -
【Swift 60秒】79 - Mutability
The final difference between classes and structs is the way they deal with constants. If you have a constant struct with a variable property, that property can't be changed because the struct itself is constant.原创 2023-01-10 10:22:20 · 437 阅读 · 0 评论 -
【Swift 60秒】78 - Deinitializers
The fourth difference between classes and structs is that classes can have `deinitializers` code that gets run when an instance of a class is destroyed.原创 2023-01-09 11:36:07 · 372 阅读 · 0 评论 -
【Swift 60秒】77 - Copying objects
The third difference between classes and structs is how they are copied. When you copy a struct, both the original and the copy are different things - changing one won’t change the other. When you copy a class, both the original and the copy point to the s原创 2023-01-06 10:28:27 · 578 阅读 · 0 评论 -
【Swift 60秒】76 - Final classes
Although class is very useful - and in fact large parts of Apple’s platforms require you to use it - sometimes you want to other developers from building their own class based on yours.Swift gives us a keyword just for this purpose: when you declare a c原创 2023-01-05 10:15:53 · 1105 阅读 · 0 评论 -
【Swift 60秒】75 - Overriding methods
Child classes can replace parent methods with their own implementations - a process known as . Here’s a trivial class with a method:If we create a new class that inherits from , it will inherit the method. So, this will print “Woof!”.Method overridin原创 2023-01-04 10:22:05 · 448 阅读 · 0 评论 -
【Swift 60秒】74 - Class inheritance
The second difference between classes and structs is that you can create a class based on an existing class - it all the properties and methods of the original class, and can add its own on top.This is called class or , the class you inherit from is call原创 2023-01-03 10:58:54 · 512 阅读 · 0 评论 -
【Swift 60秒】73 - Creating your own classes
Classes are to structs in that they allow you to create new types with properties and methods, but they have important and I’m going to walk you through each of those differences one at a time.The difference between classes and structs is that classes原创 2022-12-30 10:53:03 · 963 阅读 · 0 评论 -
【Swift 60秒】72 - Structs: Summary
You’ve made it to the end of the seventh part of this series, so let’s summarize.原创 2022-12-29 10:20:24 · 474 阅读 · 1 评论 -
【Swift 60秒】71 - Access control
Access control lets you restrict which code can use properties and methods. This is important because you might want to stop people reading a property directly, for example.We could create a struct that has an property to store their social security numb原创 2022-12-28 10:14:53 · 614 阅读 · 0 评论 -
【Swift 60秒】70 - Static properties and methods
All the properties and methods we’ve created so far have belonged to individual of structs, which means that if we had a Student struct we could create several student instances each with their own properties and methods:You can also ask Swift to share s原创 2022-12-27 10:26:37 · 254 阅读 · 0 评论 -
【Swift 60秒】69 - Lazy properties
As a performance optimization, Swift lets you some properties only when they are . As an example, consider this struct - it doesn’t do much, but in theory creating a family tree for someone takes a long time:We might use that struct as a property insid原创 2022-12-16 12:17:01 · 444 阅读 · 0 评论 -
【Swift 60秒】68 - Referring to the current instance
Inside methods you get a special constant called , which points to whatever of the struct is currently being used. This value is particularly useful when you create that have the same parameter names as your property.For example, if you create a struct原创 2022-12-15 10:14:38 · 467 阅读 · 0 评论 -
【Swift 60秒】67 - Initializers
are special methods that provide different ways to create your struct. All structs come with one by default, called their - this asks you to provide a value for each property when you create the struct.You can see this if we create a struct that has one原创 2022-12-14 10:24:07 · 607 阅读 · 0 评论 -
【Swift 60秒】66 - Properties and methods of arrays
are also structs, which means they too have their own methods and properties we can use to query and manipulate the array.Here’s a simple array to get us started:You can read the number of items in an array using its property:If you want to add a new it原创 2022-12-13 09:49:05 · 473 阅读 · 0 评论 -
【Swift 60秒】65 - Properties and methods of strings
We’ve used lots of strings so far, and it turns out they are - they have their own methods and properties we can use to query and manipulate the string.First, let’s create a test string:You can read the number of characters in a string using its propert原创 2022-12-12 10:01:45 · 558 阅读 · 0 评论 -
【Swift 60秒】64 - Mutating methods
If a struct has a property but the instance of the struct was created as a . that property be changed - the struct is , so all its properties are also constant regardless of how they were created.The problem is that when you create the struct Swift has no原创 2022-12-09 11:09:12 · 575 阅读 · 0 评论 -
【Swift 60秒】63 - Methods
Structs can have functions inside them, and those functions can use the properties of the struct as they need to. Functions inside structs are called , but they still use the same keyword.We can demonstrate this with a City struct. This will have a popula原创 2022-12-08 09:55:15 · 793 阅读 · 0 评论 -
【Swift 60秒】62 - Property observers
Property observers let you run code before or after any property changes. To demonstrate this, we’ll write a struct that tracks a task and a completion percentage:We can now create an instance of that struct and adjust its progress over time:What we wan原创 2022-12-07 10:19:04 · 427 阅读 · 0 评论 -
【Swift 60秒】61 - Computed properties
We just created a Sport struct like this:That has a name property that stores a . These are called properties, because Swift has a different kind of property called a property - a property that runs code to figure out its value.We’re going to add anothe原创 2022-12-06 10:23:39 · 498 阅读 · 0 评论 -
【Swift 60秒】60 - Creating your own structs
Swift lets you your own types in ways, of which the most common are called , or just . Structs can be given their own and , and their own , then created and used however you want.Let’s start with a simple example: we’re going to create a struct that st原创 2022-12-05 10:26:07 · 389 阅读 · 0 评论 -
【Swift 60秒】59 - Closures: Summary
You’ve made it to the end of the sixth part of this series, so let’s summarize:You can assign closures to variables, then call them later on.Closures can parameters and values, like regular functions.You can pass closures into functions as , and those cl原创 2022-12-01 12:05:10 · 734 阅读 · 0 评论
分享