What is needed in order to use NSAttributedString in a Swift application?

huangapple go评论72阅读模式
英文:

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 NSMutableAttributedStrings, 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)

huangapple
  • 本文由 发表于 2023年6月26日 21:03:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定