英文:
What is needed in order to use NSAttributedString in a Swift application?
问题
我正在创建一个简单的应用程序,用于在OSX(macOS 13.4)上读取本地iMessage数据库中的消息。这些消息存储为NSMutableAttributedString
,我理解它是NSAttributedString
的一种类型,并由Foundation框架提供。这两种类型似乎都不适用于我的Swift程序,这让我怀疑框架本身是否缺少。
一个简单的程序只需声明一个该类型的变量就会导致错误:
这个能够构建并成功运行:
print("Hello World\n")
这个却不行:
let s: NSAttributedString
导致错误
main.swift:1:8: error: 无法在作用域中找到类型 'NSAttributedString'
let s: NSAttributedString
^~~~~~~~~~~~~~~~~~
英文:
I am creating a simple application meant to read messages from the local iMessage database on OSX (macOS 13.4). These messages are stored as NSMutableAttributedString
s, which I understand to be a type of NSAttributedString, and provided by the Foundation framework. Neither of these types seem available to my Swift program, leading me to wonder if the framework itself is missing.
A simple program simply declaring a variable of that type results in an error:
This builds and runs successfully:
print("Hello World\n")
This doesn't:
let s: NSAttributedString
Producing the error
main.swift:1:8: error: cannot find type 'NSAttributedString' in scope
let s: NSAttributedString
^~~~~~~~~~~~~~~~~~
答案1
得分: 1
NSAttributedString
在 Foundation 框架中,正如其他人所提到的。它不是 Swift 语言的一部分。
在开发 iOS 或 Mac OS 应用时,几乎总是需要导入 Foundation(Mac OS)或 UIKit(iOS)。
英文:
NSAttributedString
is in the Foundation framework, as others have mentioned. It is not part of the Swift language.
When developing for iOS or Mac OS you almost always need to import either Foundation (Mac OS) or UIKit (iOS)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论