Category Archives: Programming

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

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

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

LXFoundation for Cocoa programmers

做为一个 OSX 和 iOS 软件开发者,使用 Objective-C 和 Cocoa 开发应该算是王道。尽管 Apple 为开发者提供了一系列方便的类和函数,但是绝对所有 Cocoa 开发者都遇到过这样让人头痛的问题:

*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil
*** setObjectForKey: object cannot be nil (key: username)

Cocoa 自带的容器 NSMutableArray 和 NSMutableDictionary 总是无法强壮的处理一些不正确的值。结果往往是导致程序轻则异常,重则崩溃。而更为难的是,当程序变得庞杂起来的时候,xcode 的 debugger 很难定位这样一个错误。

下面我在 GitHub 上发布一个我自己写的开源库 LXFoundation 目标就是解决这类问题。我已经把它用在了自己的软件中,目前表现稳定。 Continue reading

Cocoa中取得服务器响应头

之前有朋友问我,如果取得HTTP的服务器响应头。然后给我看了一下他的方法。是通过NSURLConnection在delegate方法connection:didReceiveResponse:里取得的,并且Request没有设定成HEAD请求。

其实在不少情况下,如果程序只是取的header不会做进一步动作。那么可以做一点优化,只做一个HEAD请求。HEAD请求
是在HTTP/1.0就定义的请求之一。而且服务器对HEAD请求响应往往更迅速。

Continue reading

Cocoa中编写你自己的变参格式化函数

问题背景

近几天,手头做的Mac下的新浪微博客户端Miao的就要收工了,在做一些代码优化方面的工作。其中就遇到了这么一个具体的问题。客户端核心引擎会统一向新浪发送不同的请求。当然这些请求是针对统一域的,但是携带不同的参数。例如:

http://api.weibo.com/statuses/friends.json?source=xxxxxx&screen_name=abc
http://api.weibo.com/statuses/friends.json?source=xxxxxx&screen_name=abc&count=100
http://api.weibo.com/statuses/public_timeline.json?source=xxxxxx&page=1&count=200
http://api.weibo.com/comments/timeline.json?source=xxxxxx&count=100

显然,程序需要在每个请求函数里建立这样一个请求的url地址。那么如何统一处理这些请求地址呢?这篇文章会向大家介绍一个C程序员都知道,但是又比较冷的技术,来处理这个问题。

Continue reading