This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func loadImages() { | |
let urls = (0..<50_000).compactMap { _ in | |
Bundle.main.url(forResource: "monalisa", withExtension: "jpg") | |
} | |
for url in urls { | |
autoreleasepool { | |
if let data = try? Data(contentsOf: url), | |
let image = UIImage(data: data) { | |
print(image.size) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Container { | |
private var backingString: String = "Hello" | |
var value: String { | |
get { | |
backingString | |
} | |
_modify { | |
yield &backingString | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
withUnsafeTemporaryAllocation(of: UInt8.self, capacity: size) { tempBuffer in | |
for i in 0..<tempBuffer.count { | |
tempBuffer[i] = UInt8(i * 2) | |
} | |
for byte in tempBuffer { | |
print(byte) // 0, 2, 4, 6, 8, 10, 12, 14 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let size = MemoryLayout<Int64>.size // 8 | |
let rawPointer = malloc(size)! // UnsafeMutableRawPointer | |
memset(rawPointer, 0, size) | |
for i in 0..<size { | |
rawPointer.assumingMemoryBound(to: UInt8.self)[i] = UInt8(i) | |
} | |
let buffer = UnsafeRawBufferPointer(start: rawPointer, count: size) | |
for byte in buffer { | |
print(byte) // 0, 1, 2, 3, 4, 5, 6, 7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Darwin | |
func loadFramework(at path: String) throws -> UnsafeMutableRawPointer? { | |
guard let handle = dlopen(path, RTLD_NOW) else { | |
if let error = dlerror() { | |
throw NSError(domain: "dlopen", code: 1, userInfo: [NSLocalizedDescriptionKey: String(cString: error)]) | |
} | |
throw NSError(domain: "dlopen", code: 2, userInfo: [NSLocalizedDescriptionKey: "Unknown error"]) | |
} | |
return handle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension MainActor { | |
public static func assumeIsolated<T : Sendable>( | |
_ operation: @MainActor () throws -> T, | |
file: StaticString = #fileID, line: UInt = #line | |
) rethrows -> T { | |
typealias YesActor = @MainActor () throws -> T | |
typealias NoActor = () throws -> T | |
let executor: Builtin.Executor = unsafe Self.shared.unownedExecutor.executor | |
guard _taskIsCurrentExecutor(executor) else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Dispatch | |
extension Task { | |
static func sync(_ code: sending () async throws(Failure) -> Success) throws(Failure) -> Success { | |
let semaphore = DispatchSemaphore(value: 0) | |
nonisolated(unsafe) var result: Result<Success, Failure>? = nil | |
withoutActuallyEscaping(code) { | |
nonisolated(unsafe) let sendableCode = $0 | |
let coreTask = Task<Void, Never>.detached(priority: .userInitiated) { @Sendable () async -> Void in | |
do { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List { | |
ForEach(0..<1_000) { _ in | |
PostCellView() | |
.listRowInsets(EdgeInsets()) | |
.listRowSeparator(.hidden) | |
.background(Color.clear) | |
} | |
} | |
.listStyle(PlainListStyle()) | |
.scrollContentBackground(.hidden) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ScrollView { | |
LazyVStack { | |
ForEach(0..<1_000) { _ in | |
PostCellView() | |
.frame(height: Double.random(in: 150...300)) | |
.clipped() | |
} | |
} | |
.scrollIndicators(.visible) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ScrollView { | |
LazyVStack { | |
ForEach(0..<1_000) { _ in | |
PostCellView() | |
} | |
} | |
} |
NewerOlder