0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[macOS]NSColorをUserDefaultsに保存する

Last updated at Posted at 2023-09-17

NSColorをUserdefautsに保存する

NSColorPanelで選択された色を保存するためにUserDefaultsを使って保存しようとしたところ
わざわざ、選択された色のRGBとAlphaの値をそれぞれ取得して配列にして保存していたが
もっと簡単な方法があったのでメモ。
参考にしたサイトは以下。

ただ情報が古いので、適宜読みかえ。

コード例

example.swift
import Cocoa

let keyString = "color"
let color = NSColor.red
let colorData = try NSKeyedArchiver.archivedData(withRootObject: color, requiringSecureCoding: false)
let data = NSData(data: colorData)
UserDefaults.standard.set(data, forKey: keyString)


var aColor : NSColor?
var theData = UserDefaults.standard.data(forKey: keyString)
if let data = theData {
    aColor = try! NSKeyedUnarchiver.unarchivedObject(ofClass: NSColor.self, from: data)!
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?