7 Design Patterns in SwiftUI

Introduction

Design patterns in SwiftUI, like in any programming framework, help organize code and promote maintainability, scalability, and readability. SwiftUI is a modern declarative framework, and it encourages the use of design patterns that fit its paradigm. Here are 7 common design patterns in SwiftUI:

  1. MVVM (Model-View-ViewModel): This is a widely used pattern in SwiftUI. It separates your code into three components:
    • Model: Represents your data and business logic.
    • View: Represents the UI and its structure.
    • ViewModel: Acts as a bridge between the Model and View, preparing data for the View and handling user interactions.
  2. Observable Object: SwiftUI provides the ObservableObject protocol to make data observable. It’s often used in conjunction with the @Published property wrapper. An observed object can notify views when its data changes.
  3. EnvironmentObject: This is a way to share data throughout your SwiftUI app. It’s especially useful for global settings or user data that many views need access to.
  4. Dependency Injection: SwiftUI makes it easy to pass data down the view hierarchy. You can inject dependencies directly into views or view models to make your code more modular and testable.
  5. Combine Framework: Combine is a powerful framework for handling asynchronous and event-driven code. It works well with SwiftUI and can be used to create reactive data flows.
  6. Coordinator: In more complex SwiftUI apps, you might use the Coordinator pattern to manage navigation and other complex UI components. This is often used with UIKit integrations.
  7. ViewModifiers: SwiftUI allows you to create reusable view modifiers to encapsulate and share view styling and behavior.
Continue reading

A Fix for the MAMBA F722 DJI MINI F35 Voltage Lose

I have been building and learning FPV drone in the last couple of weeks. It has been a bumpy journey my me. I lost my very first 5-inch quad in a tall grass field in the first 15mins flight. Now I settled on my second quad which is a 3-inch cinewhoop. It is a solid build, with many crashes it does not fail on me, except this time, after a gentle landing on a wet grass field, I lost my video feed and control.

Continue reading

How to add a column using Laravel Migration

There are many mistakes I made along the way of learning Laravel. Here I put together this post to mark one of the most important lessons I just learned. What is the right way to use Laravel Migration to add a new column without dropping a table! This article is not trying to tell you which line of code doing what job. It’s more about which step is the right way to go in a certain development context. It’s hard to grasp the full picture of the development cycle from Laravel document because it always lists all the options and tell you the details for each option, but doesn’t explain what should do next and why do it in this way.

Continue reading

CATweaker – an Xcode plugin for creating beautiful CAMediaTimingFunction curve

CATweaker

https://github.com/keefo/CATweaker

AppIcon256

Overview

A helper tool for creating beautiful CAMediaTimingFunction curve.

alt Window

CATweaker come with an Xcode plugin:

CATweakerSense for Xcode

Overview

CATweakerSense is an Xcode plugin that makes working with CAMediaTimingFunction more visual.

You might use ColorSense before. It let you pick up a color right from Xcode caret.

CATweakerSense is like ColorSense, but it lets you pick a CAMediaTimingFunction curve. When you put the caret on one of your CAMediaTimingFunction, it automatically shows the actual time curve as an overlay, and you can even adjust it on-the-fly with the curve adjuster.

The plugin also adds some items to the Edit menu to enable and to disable CATweakerSense temporarily. These menu items have no keyboard shortcuts by default, but you can set them via the system’s keyboard preferences (Xcode’s own preferences won’t show them).

alt Xcode Plugin

alt Xcode Plugin

Installation

Install via Alcatraz.

Or

Simply build the Xcode project and restart Xcode. The plugin will automatically be installed in ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins. To uninstall, just remove the plugin from there (and restart Xcode).

If you get a “Permission Denied” error while building, please see this issue.

This is tested on OS X 10.9.2 with Xcode 6.2.

Limitations

  • It only works for constant point value, something like [CAMediaTimingFunction functionWithControlPoints: abc/def : 0.1 : 0.15 : 0.9]; won’t work.

Next move

I am planing to bring keyFrame animation into this plugin. So one can create more complex animation like “spring”, “decay”, “path” through the popover adjuster. If you have some great ideas, I am more than happy to hear from you.

Credits

This work is derived from the awesome ColorSense plugin of Ole Zorn(@olemoritz). Thanks Ole!

Author

Xu Lian

I’m a Mac and iOS developer, the founder of Beyondcow, follow me on Twitter or Github.

License

CATweaker & CATweakerSense is published under MIT License

Copyright (c) 2015 Xu Lian (@lianxu)

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

XPC Service and NSTableView

Populating a NSTableView with a large amount of internet images isn’t a new problem. It may sound trivial and straight, but I see many programmers didn’t really solve it correctly. This article will introduce the correct structure for this problem and also make it as a XPC service. As the Apple document state, the two main reasons to use XPC services are privilege separation and stability. Since accessing a network resource can be slow and unstable, the XPC service is perfect for this job.

Continue reading

谨慎使用 OSX 下多触点技术

我收到过很多用户建议,软件 “这里”,“那里” 如果使用触摸板操作会多么多么非常方便。还给我支招,不同的手势操作应该出现什么操作反馈。

iOS 的多触点技术已经非常成熟, 相比较 OSX 下面的多触点技术则处于原始状态。系统提供的 API 有一些致命的 bug。这篇文章希望给所有 OSX 开发者一个提醒,不要轻易增加多触点功能。触摸板不是触摸屏!很多移动端的手势操作不是搬过来就是适合的。
Continue reading

最近的一个事:OS X Mavericks 发布和 WebKit?

大约 7 月中旬,我留意到了自己的两款 Mac 软件中涉及到 WebView 的部分相继出现了问题。具体是通过 WebView 执行 javascript 失效了。 可以执行 js 代码的方法来自 WebScriptObject 类。我尝试了各种不同的方法执行 js 代码,全部失效。包括 – callWebScriptMethod:withArguments: – evaluateWebScript: 等。
Continue reading

让NSTextView自动检测URL链接

很多新加入OSX开发的朋友经常问, NSTextView 如何显示 URL 链接呢? 其实这跟 NSTextView 没太大关系, 而是 NSAttributedString 的事情.

下面这个例子是一个 NSTextView 时实链接自动检测并高亮显示的例子, 这个例子也是 OSX 下文本语法高亮实现的基本思路.

urltextview

下面就是关键部分的一段代码:

NSError *error = NULL;
NSDataDetector *dataDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];  
NSString *string = [_textView.textStorage string];
NSArray *matches = [dataDetector matchesInString:string options:0 range:NSMakeRange(0, [string length])];
[_textView.textStorage beginEditing];
[_textView.textStorage removeAttribute:NSForegroundColorAttributeName range:NSMakeRange(0, [string length])];
[_textView.textStorage removeAttribute:NSLinkAttributeName range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult *match in matches) {
    NSRange matchRange = [match range];
    if ([match resultType] == NSTextCheckingTypeLink) {
        NSURL *url = [match URL];
        [_textView.textStorage addAttributes:@{NSLinkAttributeName:url.absoluteString} range:matchRange];
    }
}
[_textView.textStorage endEditing];

要注意的一点是, NSTextView 的 textStorage 属性是一个 NSTextStorage 对象, 它是 NSMutableAttributedString 的子类, 这里实际上是对 AttributedString 做操作. (熟悉类关系才能把握整体框架, 新手不要死盯在一个具体的方法上面)

下载例子:
https://github.com/keefo/URLTextView