Post

Replies

Boosts

Views

Activity

Defining Transformer for NSAttributedString
Coredata used to save attributed strings, but now requires a secure coding transformer. I have tried the code below, but it fails claiming This decoder will only decode classes that adopt NSSecureCoding. Class '__SwiftValue' does not adopt it The transformer has been registered and the attributes given the name 'AttributedStringToDataTransformer' Any suggestions as to what is wrong? Here's the code: class AttributedStringToDataTransformer: ValueTransformer {     override func transformedValue(_ value: Any?) - Any? {         let boxedData = try! NSKeyedArchiver.archivedData(withRootObject: value!, requiringSecureCoding: true)         return boxedData   }     override func reverseTransformedValue(_ value: Any?) - Any? {         let typedBlob = value as! Data         let data = try! NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSAttributedString.self], from: typedBlob)         return (data as! NSAttributedString)     } } extension AttributedStringToDataTransformer {     /// The name of the transformer. This is the name used to register the transformer using `ValueTransformer.setValueTrandformer(_"forName:)`.     static let name = NSValueTransformerName(rawValue: String(describing: AttributedStringToDataTransformer.self))     /// Registers the value transformer with `ValueTransformer`.     public static func register() {        ValueTransformer.setValueTransformer(AttributedStringToDataTransformer(), forName: name)     } }
1
0
869
Mar ’21
Dynamic type & custom font
If I call let font = UIFont.preferredFont(forTextStyle:.body) Then font.pointSize has value 17 If, however, I call this with a font such as Arial             let font = UIFont.fontWithNameAndTraits(fontName,                                                     size: styleAttributes.fontSize ?? 12,                                                     bold: styleAttributes.bold ?? false,                                                     italic: styleAttributes.italic ?? false)             let fontMetrics = UIFontMetrics(forTextStyle: .body)             let scaledFont = fontMetrics.scaledFont(for: font) Then scaledFont.pointSize has value 12. I was expecting 17, so I must be doing something wrong. Any suggestion? The function fontWithNameAndTraits is:     class func fontWithNameAndTraits(_ name:String, size:CGFloat, bold:Bool, italic:Bool)-UIFont {         let fontRef = UIFont.getFontRefForNameAndTraits(name, size:size, bold:bold, italic:italic)         let fontNameKey = CTFontCopyName(fontRef , kCTFontPostScriptNameKey)! as String         return UIFont(name: fontNameKey as String, size:CTFontGetSize(fontRef ))!     }
1
0
987
Apr ’21
Adding a border to a menu
Is there a way of adding a border to a SwiftUI Menu. By this I mean the menu that appears when its button is tapped, not the button itself. I've tried using .menuStyle, but this only seems to affect the button.
1
0
775
Jul ’21
IOS document icon not appearing
I can't get my app's document icon to appear for my document extension. I have tried different ways of configuring it based on Apple's documentation and various solutions posted on the internet. At first I tried adding the icon to the imported type identifier box where it says 'Add imported type identifier icons here' nothing happens when I select an icon (.png) stored in the project. I have checked the info.plist source and there is nothing there. I have since tried editing the source directly. Here are the relevant pieces of my latest attempt. Can anyone tell me where I'm going wrong. Thanks. <key>CFBundleDocumentTypes</key> <array>         <dict>             <key>CFBundleTypeIconFiles</key>             <string>writingshed.png</string>             <key>CFBundleTypeName</key>             <string>Writing Shed Data</string>             <key>LSHandlerRank</key>             <string>Owner</string>             <key>LSItemContentTypes</key>             <array>                 <string>wsd</string>             </array>         </dict> </array> <key>UTExportedTypeDeclarations</key> <array> <dict> <key>UTTypeDescription</key> <string>Writing Shed Data</string> <key>UTTypeIconFiles</key> <array> <string>writingshed.png</string> </array> <key>UTTypeIdentifier</key> <string>WritingShedData</string> <key>UTTypeTagSpecification</key> <dict> <key>public.filename-extension</key> <array> <string>wsd</string> </array> </dict>             <key>UTTypeConformsTo</key>             <array>                 <string>public.data</string>             </array> </dict> </array> <key>UTImportedTypeDeclarations</key> <array> <dict> <key>CFBundleTypeRole</key> <string>Editor</string> <key>UTTypeConformsTo</key> <array/> <key>UTTypeDescription</key> <string>Writing Shed Data</string> <key>UTTypeIconFiles</key> <array> <string>writingshed.png</string> </array> <key>UTTypeIdentifier</key> <string>WritingShedData</string> <key>UTTypeTagSpecification</key> <dict> <key>public.filename-extension</key> <array> <string>wsd</string> </array> </dict> </dict> </array>
1
1
1.4k
Oct ’21
Machine reverted to Ventura
Something weird has happened to my machine. It says it is running Ventura. But I had upgraded to Monterey some time ago. If I go to Software update there is no Monterey option, just a later version of Ventura. A Monterey disk icon appears on my desktop after startup (separate from Macintosh HD). If I open this it's empty. If I eject it it it says Preboot wasn't ejected, if I force that then it says Update wasn't ejected, then finally forcing that says VM wasn't ejected. That gets rid of it until the next restart. Has anyone any idea what is going on.
1
0
559
Jun ’23
Problem compiling @Model
I tried coding along with the WWDC23 'Dive Deeper into SwiftData' video but can't get the sample code to compile. For example the Card class fails with lots of errors associated with @Model. The first of these is: Type 'Card' does not conform to protocol 'PersistentModel' import SwiftUI import SwiftData @Model final class Card { var front: String var back: String var creationDate: Date init(front: String, back: String, creationDate: Date = .now) { self.front = front self.back = back self.creationDate = creationDate } } On the other hand the following stand alone code (in it's own project) compiles without error. So I am confused and a little fed up with Apple publishing sample code that doesn't compile. import SwiftData @Model final class Card { var front: String var back: String var creationDate: Date init(front: String, back: String, creationDate: Date = .now) { self.front = front self.back = back self.creationDate = creationDate } } struct ContentView: View { var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") } .padding() } } #Preview { ContentView() }
1
0
614
Aug ’23
Xcode 15 beta 8 link error
After installing beta 8 I'm suddenly getting the following: ld: warning: building for 'macCatalyst', but linking in dylib (/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa.tbd) built for 'macOS' Any help gratefully received.
1
1
945
Sep ’23
@Query macro vs Query struct
I have spent hours trying to get @Query macros to compile. Mostly they throw up meaningless errors for example the following produces 3 compiler errors: @Query var stylesheets: [StyleSheet] Here's the expansion. The compiler complains that 'private' can't be used here, and it can't find _stylesheets. I searched everywhere to find a resolution then I came across the Query struct. I used it as follows to replace the @Query: let query = Query(FetchDescriptor<StyleSheet>(), animation: .smooth) let styleSheets = query.wrappedValue This also solves another issue that was bugging me - how to get the context when the environment variable is often rejected. All I need to do now is write: let context = query.modelContext None of the WWDC23 SwiftData videos mentions the use of the struct, which is a shame. It feels much like the CoreData approach to fetching data. I hope this helps some of you.
1
0
900
May ’24
Deleting CloudKit containers
When I go to Settings and tap on the account, choose iCloud>manage account storage and try to delete the storage associated with an app id developer mode, it doesn't give me the option to delete the data. I have tried using the CloudKit Console to delete the container data, but the entry for the app is still there. How do I delete it?
1
0
1k
May ’24
View update issues + DisclosureGroup issue
My code was working perfectly well until the latest Xcode update. Suddenly the DisclosureGroup stopped working, causing the app to freeze. Also there seems to have been a change to the way SwiftUI tracks view updates because some of my code went into a screaming loop thinking a view was constantly changing. Developing code is hard enough without these problems coming out of nowhere.
1
0
522
May ’24
Reset Developer Id Password
I managed to lose my password in a mixup with my password manager. It has taken me a few hours to work out how to reset it. Following the forgotten password link on the sign in page only takes you to a point where you are asked to reset the password on your iCloud user id. If they are one and the same that's ok. Mine aren't. I eventually found the answer: in the settings app choose developer 2 scroll to the end and choose the sandbox apple account 3. choose this and reset
1
0
521
Jan ’25