Post

Replies

Boosts

Views

Activity

Reply to How to operate a image file
the search terms you are looking for are NSImage (macOS) or UIImage (iOS/iPadOS). Images have methods for loading from and writing to a URL. Images can be rendered into a CGContext. CGContexts provide access to their pixel buffers (and various higher level operations which manipulate those buffers).
Topic: Programming Languages SubTopic: Swift Tags:
May ’23
Reply to How to operate a image file
a URL can point at a local file (the scheme is 'file', so such URLs begin "file://"). For example, if you use NSOpenPanel on macOS to enable the user to select the file(s) to open, you'll get an array of URLs from the panel. See https://developer.apple.com/documentation/appkit/nsopenpanel. NSImage supports jpeg format input.
Topic: Programming Languages SubTopic: Swift Tags:
May ’23
Reply to Swift language, how to convert an array of int type to data type
plain old data in Swift arrays are contiguous in memory. If the type you are storing were more complex, you may need to use ContiguousArray instead of Array. There are lots of withUnsafeXXX functions, both free and member functions; it took me quite a while to find the right ones. This code generates your array of Int values, makes a Data from the array's data, then makes a new Array from the Data's data. There doesn't seem to be a way to make an Array or even a ContiguousArray directly from a buffer pointer. import Foundation let sizeofElement = MemoryLayout<Int>.size // create array (example is twelve Ints from 1 to 12 var array = Array(1...12) // create a Data from the bytes in the original array var data = Data() array.withUnsafeBufferPointer { data = Data(bytes: $0.baseAddress!, count: $0.count * MemoryLayout<Int>.size) } // construct a new array by walking through the bytes of data, one Float at a time // start with an empty array, which we can manipulate in the closure below var newArray: [Int] = [] data.withUnsafeBytes { rawBufferPtr in rawBufferPtr.withMemoryRebound(to: Int.self) { buffer in // access it as contiguous Int values var iter = buffer.makeIterator() while let aNumber = iter.next() { newArray.append(aNumber) } } } print(array) print(newArray)
Topic: Programming Languages SubTopic: Swift Tags:
May ’23
Reply to Project Won't Build with .h File
Don't you hate it when this happens? Yes, there is a newer version of Xcode (14.3.1), but I doubt that the problem is that you are not using the latest version, given that your project didn't change. I suggest you take a deep breath, boot into Recovery mode and run Disk First Aid. Perhaps something is messed up in your file system.
Jun ’23
Reply to When to use utf8 vs utf16 vs ascii character codes?
If you're doing this for a user-entered string, don't worry about how they're encoded. CNPhoneNumber.stringValue is of type String A String is a collection of Characters You could iterate through the String by hand, but it is awkward. Swift offers compactMap, which will give you an array of single-character strings. A new String can be created from this Array. for example let likeAPhoneNumber = "(451)∕234-141😃0" let newNumberArray = likeAPhoneNumber.compactMap { $0.isNumber ? $0 : nil } let newNumber = String(newNumberArray) print (newNumber)prints 4512341410 None of this is high-performance, because iterating through a Unicode string is non-trivial.
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’23
Reply to How to implement drag and drop of SF Symbols within a SwiftUI grid?
the 'drop' part of a DragGesture can be handled by the DragGesture.onEnded closure. In your .onChanged closure you can figure out where the drag is currently (value.location), so you could highlight legal destinations. In your .onEnded closure you figure out whether the move was legal and update your model accordingly. Your question is a bit broad in scope. Try to narrow it down to something where you can show a piece of self-contained code, and explain what you expect, and what actually happens.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’23
Reply to AppleScript. How do I get a list of all files in all subfolders without any conditions?
something like this tell application "Finder" get entire contents of alias "Users:username:Documents:somefolder" end tell but be warned that if somefolder is large, this will take a very long time. This asks the Finder to do the work on behalf of your script. The Finder doesn't show hidden files, so they don't appear in the entire contents result. There's a post here which uses System Events to do the same thing, with more code, but much quicker (and you'd have to filter out invisible files yourself) https://www.macscripter.net/t/getting-all-files-list-of-directory-and-of-its-subdirectories/71573/4 Did you search for "AppleScript list files and folders"? It wasn't difficult to find these options. Perhaps you did find them but were not happy with the result in your application? If so, please describe what exactly you did try to do, and how it fell short of your expectations.
Topic: App & System Services SubTopic: Core OS Tags:
Jun ’23