Initial commit: Original Python LLMTester project
This commit is contained in:
@@ -0,0 +1,988 @@
|
||||
# Automated Diagnostic Report: phi-3-medium-128k-instruct
|
||||
|
||||
---
|
||||
|
||||
## Performance Summary
|
||||
|
||||
<!--SUMMARY_START-->
|
||||
* **Average Tokens/s:** 8.74
|
||||
* **Average Time to First Token:** 0.0s
|
||||
* **Total Tokens Generated:** 6069
|
||||
<!--SUMMARY_END-->
|
||||
|
||||
## Qualitative Analysis
|
||||
|
||||
*(Manual grading and analysis of the responses is required to determine the final letter grade.)*
|
||||
|
||||
---
|
||||
|
||||
## Test 1: Write a Swift function that solves the FizzBuzz problem.
|
||||
|
||||
*Source:* `test1.txt`
|
||||
|
||||
### Prompt:
|
||||
```swift
|
||||
Write a Swift function that solves the FizzBuzz problem.
|
||||
|
||||
The function must have the exact signature:
|
||||
`func generateFizzBuzz(upTo max: Int) -> [String]`
|
||||
|
||||
It must not print directly to the console. Instead, it should return an array of strings.
|
||||
|
||||
Rules:
|
||||
1. For numbers divisible by 3, the string should be "Fizz".
|
||||
2. For numbers divisible by 5, the string should be "Buzz".
|
||||
3. For numbers divisible by both 3 and 5, the string should be "FizzBuzz".
|
||||
4. Otherwise, it should be the number itself as a string.
|
||||
|
||||
Include a complete documentation comment (`///`) explaining what the function does, its parameters, and what it returns.
|
||||
```
|
||||
|
||||
### Response:
|
||||
|
||||
```swift
|
||||
/// Generates an array of FizzBuzz strings up to a given maximum value (inclusive).
|
||||
/// - Parameters:
|
||||
/// - max: The maximum number in the sequence.
|
||||
/// - Returns: An array of strings representing the FizzBuzz sequence from 1 to `max`.
|
||||
func generateFizzBuzz(upTo max: Int) -> [String] {
|
||||
var result = [String]()
|
||||
|
||||
for i in 1...max {
|
||||
let str = ""
|
||||
|
||||
if i % 3 == 0 {
|
||||
str += "Fizz"
|
||||
}
|
||||
|
||||
if i % 5 == 0 {
|
||||
str += "Buzz"
|
||||
}
|
||||
|
||||
result.append(str.isEmpty ? String(i) : str)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
```
|
||||
|
||||
### Performance Metrics:
|
||||
* **Tokens/s:** 7.91
|
||||
* **Total Tokens:** 195
|
||||
* **Time to First Token:** 0.0s
|
||||
* **Stop Reason:** stop
|
||||
|
||||
---
|
||||
|
||||
## Test 2: Create a complete SwiftUI view for a user profile card.
|
||||
|
||||
*Source:* `test2.txt`
|
||||
|
||||
### Prompt:
|
||||
```swift
|
||||
Create a complete SwiftUI view for a user profile card.
|
||||
|
||||
The view should be named `UserProfileCard` and be initialized with a simple `User` struct containing `name: String`, `handle: String`, and `avatarURL: URL`.
|
||||
|
||||
Requirements:
|
||||
- Use an `AsyncImage` to load the user's avatar from the URL.
|
||||
- Display the user's name in a `.headline` font and their handle (e.g., "@username") in a `.subheadline` font with a secondary color.
|
||||
- Include a "Follow" button that uses the "person.badge.plus" SF Symbol.
|
||||
- Arrange the elements cleanly inside an HStack with appropriate spacing.
|
||||
- The entire card should have a subtle shadow and rounded corners.
|
||||
```
|
||||
|
||||
### Response:
|
||||
|
||||
```swift
|
||||
|
||||
import SwiftUI
|
||||
|
||||
|
||||
struct UserProfileCard: View {
|
||||
|
||||
let user: User
|
||||
|
||||
|
||||
var body: some View {
|
||||
|
||||
HStack(alignment: .leading, spacing: 10) {
|
||||
|
||||
AsyncImage(url: user.avatarURL)
|
||||
|
||||
.resizable()
|
||||
|
||||
.scaledToFill()
|
||||
|
||||
.cornerRadius(8)
|
||||
|
||||
.frame(width: 43, height: 43)
|
||||
|
||||
.clipShape(Circle())
|
||||
|
||||
Text(user.name)
|
||||
|
||||
.font(.headline)
|
||||
|
||||
Spacer()
|
||||
|
||||
HStack {
|
||||
|
||||
Text("\(user.handle)")
|
||||
|
||||
.font(.subheadline)
|
||||
|
||||
.foregroundColor(.secondary)
|
||||
|
||||
Button(action: {}) {
|
||||
|
||||
Image(systemName: "person.badge.plus")
|
||||
|
||||
.foregroundColor(.accentColor)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
}
|
||||
|
||||
.shadow(radius: 4, y_offset: 2)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
struct UserProfileCard_Previews: PreviewProvider {
|
||||
|
||||
static var prefs: [String : Any] = ["com.apple.developer.xpc-process-peruser-ids.yournamehere": "1"]
|
||||
|
||||
|
||||
static var sampleUser: User = (name: "Jane Doe", handle: "@jdoe", avatarURL: URL(string: "https://example.com/avatars/jdoe.jpg")!)
|
||||
|
||||
|
||||
static var sampleView = UserProfileCard(user: .sampleUser)
|
||||
|
||||
}
|
||||
|
||||
|
||||
struct User {
|
||||
|
||||
let name: String
|
||||
|
||||
let handle: String
|
||||
|
||||
let avatarURL: URL
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Performance Metrics:
|
||||
* **Tokens/s:** 9.86
|
||||
* **Total Tokens:** 450
|
||||
* **Time to First Token:** 0.0s
|
||||
* **Stop Reason:** stop
|
||||
|
||||
---
|
||||
|
||||
## Test 3: I have this old networking code that uses a completion handler. Please refactor it to use modern Swift Concurrency with async/await.
|
||||
|
||||
*Source:* `test3.txt`
|
||||
|
||||
### Prompt:
|
||||
```swift
|
||||
I have this old networking code that uses a completion handler. Please refactor it to use modern Swift Concurrency with async/await.
|
||||
|
||||
The new function should be marked with `async throws` and return a `[Post]` object upon success. Also, include an example of how to call this new async function inside a `Task` with a `do-catch` block.
|
||||
|
||||
**Old Code:**
|
||||
struct Post: Codable {
|
||||
let id: Int
|
||||
let title: String
|
||||
}
|
||||
|
||||
func fetchPosts(completion: @escaping (Result<[Post], Error>) -> Void) {
|
||||
guard let url = URL(string: "https://api.example.com/posts") else { return }
|
||||
URLSession.shared.dataTask(with: url) { data, _, error in
|
||||
if let error = error { completion(.failure(error)); return }
|
||||
guard let data = data else { return }
|
||||
do {
|
||||
let posts = try JSONDecoder().decode([Post].self, from: data)
|
||||
completion(.success(posts))
|
||||
} catch {
|
||||
completion(.failure(error))
|
||||
}
|
||||
}.resume()
|
||||
}
|
||||
```
|
||||
|
||||
### Response:
|
||||
|
||||
```swift
|
||||
import Foundation
|
||||
|
||||
struct Post: Codable {
|
||||
let id: Int
|
||||
let title: String
|
||||
}
|
||||
|
||||
func fetchPostsAsync() async throws -> [Post] {
|
||||
guard let url = URL(string: "https://api.example.com/posts") else { return [] }
|
||||
|
||||
let dataTask = URLSession.shared.dataTaskPublisher(for: url)
|
||||
.sink { data, response, _ in
|
||||
if let errorResponse = response as? URLErrorDomain.URLError, case .badServerResponse = errorResponse.code {
|
||||
throw NSError(domain: NSURLErrorDomain, code: errorResponse.code, userInfo: [NSURLErrorFailingURLErrorKey: url])!
|
||||
} else if let errorResponse = response as? URLErrorDomain.URLError, case .timedOut = errorResponse.code {
|
||||
throw NSError(domain: NSURLErrorDomain, code: errorResponse.code, userInfo: [NSURLErrorFailingURLErrorKey: url])!
|
||||
} else if let errorResponse = response as? URLErrorDomain.URLError, case .otherError = errorResponse.code {
|
||||
throw NSError(domain: NSURLErrorDomain, code: errorResponse.code, userInfo: [NSURLErrorFailingURLErrorKey: url])!
|
||||
} else if let httpURLResponse = response as? HTTPURLResponse, !(200...299).contains(httpURLResponse.statusCode) {
|
||||
throw NSError(domain: NSURLErrorDomain, code: -1, userInfo: [NSURLErrorFailingURLErrorKey: url])!
|
||||
} else if let httpURLResponse = response as? HTTPURLResponse, !(200...299).contains(httpURLResponse.statusCode) {
|
||||
throw NSError(domain: NSURLErrorDomain, code: -1, userInfo: [NSURLErrorFailingURLErrorKey: url])!
|
||||
} else if let httpURLResponse = response as? HTTPURLResponse {
|
||||
switch httpURLResponse.statusCode {
|
||||
case 400...499:
|
||||
throw NSError(domain: NSURLErrorDomain, code: -1, userInfo: [NSURLErrorFailingURLErrorKey: url])!
|
||||
default: break // No error for successful responses
|
||||
}
|
||||
} else {
|
||||
throw NSError(domain: NSURLErrorDomain, code: -1, userInfo: [NSURLErrorFailingURLErrorKey: url])!
|
||||
}
|
||||
}.eraseToAnyPublisher()
|
||||
|
||||
let (result) = await dataTask.sink<Data> { data in
|
||||
return try JSONDecoder().decode([Post].self, from: data).mapError(convertFromURLErrorCode)
|
||||
}.receive().cancellable().fuseForever()
|
||||
|
||||
return result.value ?? []
|
||||
}
|
||||
|
||||
func convertFromURLErrorCode(_ error: Error) -> Error {
|
||||
switch error as? NSError {
|
||||
case let e as NSError & URLErrorDomain.URLError?:
|
||||
if let code = e.code, let domain = e.domain {
|
||||
return NSError(domain: NSURLErrorDomain, code: code, userInfo: [NSURLErrorFailingURLErrorKey: (e as! URLError).url])!
|
||||
} else {
|
||||
return error
|
||||
}
|
||||
default:
|
||||
return error
|
||||
}
|
||||
}
|
||||
```
|
||||
**Example of calling the new async function inside a `Task` with a `do-catch` block:**
|
||||
```swift
|
||||
Task { do {
|
||||
let posts = try await fetchPostsAsync()
|
||||
// Handle successful response here
|
||||
} catch {
|
||||
print(error) // Print error message if any
|
||||
}
|
||||
```
|
||||
|
||||
### Performance Metrics:
|
||||
* **Tokens/s:** 9.58
|
||||
* **Total Tokens:** 896
|
||||
* **Time to First Token:** 0.0s
|
||||
* **Stop Reason:** stop
|
||||
|
||||
---
|
||||
|
||||
## Test 4: Create a complete SwiftData example for a macOS 26 app.
|
||||
|
||||
*Source:* `test4.txt`
|
||||
|
||||
### Prompt:
|
||||
```swift
|
||||
Create a complete SwiftData example for a macOS 26 app.
|
||||
|
||||
You must define two models: `Author` and `Book`. An `Author` can have many `Book`s (a one-to-many relationship).
|
||||
|
||||
Then, create a SwiftUI view that displays a list of all authors. When an author's name is tapped, it should navigate to a detail view that shows a list of all the books written by that specific author. Use a SwiftData `@Query` to fetch the data.
|
||||
```
|
||||
|
||||
### Response:
|
||||
|
||||
1. Define the `Author` and `Book` models with Core Data:
|
||||
|
||||
```swift
|
||||
import CoreData
|
||||
|
||||
class Author: NSManagedObject {
|
||||
@NSManaged var name: String
|
||||
@NSManaged var books: Set<Book>
|
||||
}
|
||||
|
||||
class Book: NSManagedObject {
|
||||
@NSManaged var title: String
|
||||
@NSManaged var author: Author?
|
||||
}
|
||||
```
|
||||
|
||||
2. Create a Core Data stack and fetch request for the `Author` entity:
|
||||
|
||||
```swift
|
||||
import SwiftData
|
||||
|
||||
struct ContentView: View {
|
||||
let context = PersistenceController.shared.container.persistentStoreCoordinator?.managedObjectContext!
|
||||
|
||||
var body: some View {
|
||||
List(fetchRequest, id: \.name) { author in
|
||||
NavigationLink(destination: AuthorDetailView(author: author)) {
|
||||
Text(author.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
3. Create the `Author` fetch request and sort it by name:
|
||||
|
||||
```swift
|
||||
let authors = FetchRequest<Author>(fetchLimit: .few, sortDescriptors: [NSSortDescriptor(keyPath: \Author.name, ascending: true)])
|
||||
.query { query in
|
||||
let predicate = NSPredicate(format: "self.books.@count > 0")
|
||||
return query.predicate == nil ? predicate : nil
|
||||
}
|
||||
```
|
||||
|
||||
4. Create the `Book` fetch request and sort it by title:
|
||||
|
||||
```swift
|
||||
let books = FetchRequest<Book>(fetchLimit: .few, sortDescriptors: [NSSortDescriptor(keyPath: \Book.title, ascending: true)])
|
||||
.query { query in
|
||||
return query.predicate == nil ? NSPredicate(format: "self.author != NULL") : nil
|
||||
}
|
||||
```
|
||||
|
||||
5. Create the `AuthorDetailView` that displays a list of all books written by the specific author:
|
||||
|
||||
```swift
|
||||
struct AuthorDetailView: View {
|
||||
let author: Author
|
||||
|
||||
var body: some View {
|
||||
List(books, id: \.title) { book in
|
||||
Text(book.title)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
6. Create a `PersistenceController` to manage the Core Data stack and SwiftData contexts:
|
||||
|
||||
```swift
|
||||
class PersistenceController: NSObject, ContextProvider {
|
||||
static let shared = PersistenceController()
|
||||
|
||||
private init() {}
|
||||
|
||||
func getContext(in persistenceContainer container: Any) -> CoreData.PersistentContainer? {
|
||||
guard let persistentStoreCoordinator = container.persistentStoreCoordinator else { return nil }
|
||||
|
||||
let containerOptions: [NSPersistentStoreOptionKey: Any] = [
|
||||
NSPersistentStoreOptionKey.shouldCreateStorageIfNeeded: true,
|
||||
NSPersistentStoreOptionKey.isNewTransaction: false,
|
||||
NSPersistentStoreOptionKey.mergeDiscoveries: true
|
||||
]
|
||||
|
||||
let containerURL = URL(fileURLWithPath: "path/to/your/CoreDataStack")
|
||||
|
||||
do {
|
||||
let persistentContainer = try NSPersistentContainer(persistentStoreDescriptions: [NSManagedObjectContextConfiguration(url: containerURL, options: containerOptions)])
|
||||
|
||||
for description in persistentContainer.persistentStoreDescriptions {
|
||||
try persistentContainer.loadPersistentStores { _, error in
|
||||
if let error = error as? NSFaultError {
|
||||
fatalError("Unresolved Core Data error \(error)")
|
||||
} else if let error = error as? ErrorType {
|
||||
print(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return persistentContainer.viewContext
|
||||
} catch {
|
||||
fatalError("Failed to initialize the persistence container: \(error)")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
7. Run your macOS app and test the functionality by adding some authors and books in the Core Data model editor.
|
||||
|
||||
### Performance Metrics:
|
||||
* **Tokens/s:** 9.8
|
||||
* **Total Tokens:** 917
|
||||
* **Time to First Token:** 0.0s
|
||||
* **Stop Reason:** stop
|
||||
|
||||
---
|
||||
|
||||
## Test 5: Write a thread-safe counter class in Swift using an `actor`.
|
||||
|
||||
*Source:* `test5.txt`
|
||||
|
||||
### Prompt:
|
||||
```swift
|
||||
Write a thread-safe counter class in Swift using an `actor`.
|
||||
|
||||
The actor should be named `SafeCounter`. It must have a private property to hold the current count and two public methods:
|
||||
1. `func increment() async`
|
||||
2. `func getCount() async -> Int`
|
||||
|
||||
Also, provide a simple example of how to create an instance of the actor and call its methods concurrently from two different `Task`s.
|
||||
```
|
||||
|
||||
### Response:
|
||||
|
||||
```swift
|
||||
import Foundation
|
||||
|
||||
actor SafeCounter {
|
||||
private(set) var count = 0
|
||||
|
||||
func increment() async {
|
||||
self.count += 1
|
||||
}
|
||||
|
||||
func getCount() async -> Int {
|
||||
return await actorScope.run {
|
||||
return self.count
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let counterActor = SafeCounter()
|
||||
|
||||
Task {
|
||||
for _ in 0..<5 {
|
||||
await counterActor.increment()
|
||||
}
|
||||
}
|
||||
|
||||
Task {
|
||||
let count1 = await counterActor.getCount()
|
||||
print("Count from Task 1: \(count1)")
|
||||
|
||||
let count2 = await counterActor.getCount()
|
||||
print("Count from Task 2: \(count2)")
|
||||
}
|
||||
```
|
||||
|
||||
In this example, we define a `SafeCounter` actor with two methods: `increment()` and `getCount()`. The `increment()` method simply increments the private count property by one. The `getCount()` method returns the current value of the count asynchronously using an `actorScope`.
|
||||
|
||||
We then create an instance of the `SafeCounter` actor called `counterActor`. In two separate tasks, we increment and retrieve the counter's value concurrently to demonstrate thread safety.
|
||||
|
||||
### Performance Metrics:
|
||||
* **Tokens/s:** 9.57
|
||||
* **Total Tokens:** 292
|
||||
* **Time to First Token:** 0.0s
|
||||
* **Stop Reason:** stop
|
||||
|
||||
---
|
||||
|
||||
## Test 6: Create the code for a simple, non-configurable watchOS 26 complication that displays the current date in the `.accessoryRectangular` family.
|
||||
|
||||
*Source:* `test6.txt`
|
||||
|
||||
### Prompt:
|
||||
```swift
|
||||
Create the code for a simple, non-configurable watchOS 26 complication that displays the current date in the `.accessoryRectangular` family.
|
||||
|
||||
The code must include:
|
||||
1. A `TimelineProvider` to generate the timeline entries.
|
||||
2. A `TimelineEntry` struct.
|
||||
3. The main `Widget` struct.
|
||||
4. A SwiftUI view for the complication's appearance.
|
||||
|
||||
Ensure the code correctly uses the modern WidgetKit framework.
|
||||
```
|
||||
|
||||
### Response:
|
||||
|
||||
```swift
|
||||
|
||||
import WidgetKit
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
// TimelineEntry to represent a single entry in our timeline
|
||||
|
||||
struct DateTimelineEntry: TimelineEntry {
|
||||
|
||||
let date: Date
|
||||
|
||||
var preferredSize: CGSize { .init(width: 200, height: 100) }
|
||||
|
||||
|
||||
func getFutureItems(_ interval: Int) -> [Date]? {
|
||||
|
||||
return [] // No future items needed for a static complication.
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// TimelineProvider to provide the timeline entries
|
||||
|
||||
class DateTimelineProvider: TimelineProvider {
|
||||
|
||||
typealias Entry = DateTimelineEntry
|
||||
|
||||
|
||||
func getEntries(for queue) -> [DateTimelineEntry]? {
|
||||
|
||||
let now = Calendar.current.dateComponents([.year, .month], from: Date())
|
||||
|
||||
let dateString = "\(now.year)-\(now.month)" // Simplified for example purposes
|
||||
|
||||
return [DateTimelineEntry(date: Date(timeIntervalSinceNow: -60 * 24))] // Static entry representing the current day
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Widget struct that conforms to ComplicationConfigurable and TimelineProvider
|
||||
|
||||
class DateComplicationWidget: Widget {
|
||||
|
||||
typealias Entry = DateTimelineEntry
|
||||
|
||||
|
||||
required init(configuration: Configuration) {
|
||||
|
||||
super.init(configuration: configuration, provider: DateTimelineProvider())
|
||||
|
||||
}
|
||||
|
||||
|
||||
func timeline(for context: TimelineContext<DateTimelineEntry>) -> some View? {
|
||||
|
||||
return GeometryReader { geometry in
|
||||
|
||||
VStack {
|
||||
|
||||
Text("\(context.entry.date, formatter: .shortMedium)") // Display the date using a short medium style
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Companion view for the widget's appearance in the complication bar
|
||||
|
||||
struct DateComplicationWidgetView: UIViewRepresentable {
|
||||
|
||||
var context: Context
|
||||
|
||||
|
||||
func makeUIView(context: Context) -> UIView {
|
||||
|
||||
let dateLabel = UILabel()
|
||||
|
||||
dateLabel.textAlignment = .center
|
||||
|
||||
return dateLabel
|
||||
|
||||
}
|
||||
|
||||
|
||||
func updateUIView(_ view: UIView, context: Context) {
|
||||
|
||||
// Update the label with the current date here if needed
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Companion struct for the widget's appearance in the complication bar
|
||||
|
||||
struct DateComplicationWidget_Configuration: Configuration.EstimatedTimelineGroupingConfig, TimelineGroupingConfig {
|
||||
|
||||
static var estimatedDurationForEntry: Int { 140 }
|
||||
|
||||
static var groupingPeriod: Int { 24 * 60 * 60 } // One day in seconds
|
||||
|
||||
}
|
||||
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
|
||||
export class DateComplicationWidgetProvider: TimelineProvider {
|
||||
|
||||
typealias Entry = DateTimelineEntry
|
||||
|
||||
|
||||
func getEntries(for queue) -> [DateTimelineEntry]? {
|
||||
|
||||
return [] // No future items needed for a static complication.
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
|
||||
export class DateComplicationWidget: ComplicationConfigurable {
|
||||
|
||||
var configuration = TimelineGroupingConfig()
|
||||
|
||||
|
||||
func configure(_ cfg: Configuration) -> Void {
|
||||
|
||||
self.configuration = cfg
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Performance Metrics:
|
||||
* **Tokens/s:** 8.72
|
||||
* **Total Tokens:** 823
|
||||
* **Time to First Token:** 0.0s
|
||||
* **Stop Reason:** stop
|
||||
|
||||
---
|
||||
|
||||
## Test 7: Refactor this SwiftUI view and its `ObservableObject` to use the modern `@Observable` macro introduced in Swift 5.9. The refactored code should be simpler and should not use the `@Published` property wrapper or the `ObservableObject` protocol.
|
||||
|
||||
*Source:* `test7.txt`
|
||||
|
||||
### Prompt:
|
||||
```swift
|
||||
Refactor this SwiftUI view and its `ObservableObject` to use the modern `@Observable` macro introduced in Swift 5.9. The refactored code should be simpler and should not use the `@Published` property wrapper or the `ObservableObject` protocol.
|
||||
|
||||
**Old Code:**
|
||||
class UserSettings: ObservableObject {
|
||||
@Published var score: Int = 0
|
||||
@Published var username: String = "Guest"
|
||||
}
|
||||
|
||||
struct SettingsView: View {
|
||||
@StateObject private var settings = UserSettings()
|
||||
var body: some View {
|
||||
Form {
|
||||
TextField("Username", text: $settings.username)
|
||||
Stepper("Score: \(settings.score)", value: $settings.score)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Response:
|
||||
|
||||
```swift
|
||||
import SwiftUI
|
||||
|
||||
struct UserSettings: ObservableObject {
|
||||
@Published var score: Int = 0
|
||||
@Published var username: String = "Guest"
|
||||
|
||||
init() {}
|
||||
}
|
||||
|
||||
struct SettingsView: View {
|
||||
@StateObject private var settings = UserSettings()
|
||||
var body: some View {
|
||||
Form {
|
||||
TextField("Username", text: $settings.username)
|
||||
.keyboardType(.text)
|
||||
|
||||
Stepper("Score: \(settings.score)", value: $settings.score)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
In this refactored code, we use the `@StateObject` property wrapper instead of `@Published`. The `UserSettings` class is now a struct conforming to the `ObservableObject` protocol and has two properties with the `@Published` property wrapper.
|
||||
|
||||
The `SettingsView` struct uses the `@StateObject` property wrapper for its `settings` property, which ensures that it maintains ownership of the object across view reloads. The rest of the code remains unchanged.
|
||||
|
||||
### Performance Metrics:
|
||||
* **Tokens/s:** 7.49
|
||||
* **Total Tokens:** 242
|
||||
* **Time to First Token:** 0.0s
|
||||
* **Stop Reason:** stop
|
||||
|
||||
---
|
||||
|
||||
## Test 8: Create the code for a simple, configurable WidgetKit widget for iOS 26. The widget should display a single "Note" string. The configuration logic must use the modern App Intents framework, allowing the user to edit the note's text directly from the widget's configuration screen. Do not use the old `INIntent` based system.
|
||||
|
||||
*Source:* `test8.txt`
|
||||
|
||||
### Prompt:
|
||||
```swift
|
||||
Create the code for a simple, configurable WidgetKit widget for iOS 26. The widget should display a single "Note" string. The configuration logic must use the modern App Intents framework, allowing the user to edit the note's text directly from the widget's configuration screen. Do not use the old `INIntent` based system.
|
||||
```
|
||||
|
||||
### Response:
|
||||
|
||||
To create a simple configurable WidgetKit widget for iOS 26 that displays a single "Note" string using the modern App Intents framework, follow these steps:
|
||||
|
||||
1. Create a new SwiftUI View called NoteWidgetView and make it conform to `Widget` protocol with `@available(iOS 14.0, *)` attribute. This view will be responsible for displaying the note text and handling user interactions.
|
||||
2. In your AppDelegate or SceneDelegate file, register the widget by implementing the `widgetBundleProvider(_:for:configurationName:)` method to return a Bundle containing the NoteWidgetView's SwiftUI code.
|
||||
3. Create an Intent subclass called EditNoteIntent that inherits from UIResponder and conforms to Intents protocol with `@available(iOS 14.0, *)` attribute. This intent will be used for editing the note text in the widget configuration screen.
|
||||
4. In your AppDelegate or SceneDelegate file, implement the `intent(_:configurationName:contextForConfigurationIntent:completionHandler:)` method to handle EditNoteIntent and return a UIHostingController containing the SwiftUI code for the edit view.
|
||||
5. Create an Intent subclass called ShowWidgetIntent that inherits from UIResponder and conforms to Intents protocol with `@available(iOS 14.0, *)` attribute. This intent will be used for showing the widget on the home screen.
|
||||
6. In your AppDelegate or SceneDelegate file, implement the `intent(_:configurationName:contextForConfigurationIntent:completionHandler:)` method to handle ShowWidgetIntent and return a UIHostingController containing the SwiftUI code for the main view.
|
||||
7. Create an Intent subclass called CloseWidgetIntent that inherits from UIResponder and conforms to Intents protocol with `@available(iOS 14.0, *)` attribute. This intent will be used for closing the widget on the home screen.
|
||||
8. In your AppDelegate or SceneDelegate file, implement the `intent(_:configurationName:contextForConfigurationIntent:completionHandler:)` method to handle CloseWidgetIntent and return a UIHostingController containing the SwiftUI code for the close view.
|
||||
9. Implement the logic in NoteWidgetView's body to display the note text using Text(note) or any other desired layout. Add buttons or gestures as needed to navigate between different views (edit, show widget, and close widget).
|
||||
10. In your AppDelegate or SceneDelegate file, implement the `intent(_:configurationName:contextForConfigurationIntent:completionHandler:)` method for ShowWidgetIntent to return a UIHostingController containing NoteWidgetView's SwiftUI code when the user wants to add the widget to their home screen.
|
||||
11. Implement the logic in EditNoteIntent and CloseWidgetIntent views using SwiftUI, allowing users to edit or close the widget on the home screen.
|
||||
|
||||
Here is an example of how your code might look:
|
||||
|
||||
```swift
|
||||
import WidgetKit
|
||||
import SwiftUI
|
||||
|
||||
struct NoteWidgetView: View {
|
||||
@Binding var note: String
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 20) {
|
||||
Text(note)
|
||||
.font(.systemFont(ofSize: 36))
|
||||
|
||||
Button("Edit") {
|
||||
// Navigate to EditNoteIntent view for editing the note text.
|
||||
}
|
||||
|
||||
Button("Show Widget") {
|
||||
// Show widget on home screen using ShowWidgetIntent.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class EditNoteIntent: UIResponder, Intents<EditNoteIntent> {
|
||||
var configurationName = "edit-note"
|
||||
|
||||
func configure(_ intentContext: Context) -> Bool {
|
||||
// Implement the logic to edit note text here.
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
class ShowWidgetIntent: UIResponder, Intents<ShowWidgetIntent> {
|
||||
var configurationName = "show-widget"
|
||||
|
||||
func configure(_ intentContext: Context) -> Bool {
|
||||
// Implement the logic to show widget on home screen here.
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
class CloseWidgetIntent: UIResponder, Intents<CloseWidgetIntent> {
|
||||
var configurationName = "close-widget"
|
||||
|
||||
func configure(_ intentContext: Context) -> Bool {
|
||||
// Implement the logic to close widget on home screen here.
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Remember that this is just a basic example, and you may need to customize it further based on your specific requirements.
|
||||
|
||||
### Performance Metrics:
|
||||
* **Tokens/s:** 8.22
|
||||
* **Total Tokens:** 1011
|
||||
* **Time to First Token:** 0.0s
|
||||
* **Stop Reason:** stop
|
||||
|
||||
---
|
||||
|
||||
## Test 9: Write a complete SwiftUI view for visionOS 26 that presents a fully immersive space when a button is tapped.
|
||||
|
||||
*Source:* `test9.txt`
|
||||
|
||||
### Prompt:
|
||||
```swift
|
||||
Write a complete SwiftUI view for visionOS 26 that presents a fully immersive space when a button is tapped.
|
||||
|
||||
The view must:
|
||||
1. Have a button labeled "Enter Immersive Space".
|
||||
2. Use the `@Environment(\.openImmersiveSpace)` and `@Environment(\.dismissImmersiveSpace)` variables.
|
||||
3. Be able to open an immersive space with the ID "MyImmersiveScene".
|
||||
4. Include a second button inside the immersive space to dismiss it.
|
||||
```
|
||||
|
||||
### Response:
|
||||
|
||||
```swift
|
||||
import SwiftUI
|
||||
|
||||
struct ImmersiveView: View {
|
||||
@Environment(\.openImmersiveSpace) var openImmersiveSpace
|
||||
@Environment(\.dismissImmersiveSpace) var dismissImmersiveSpace
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
Button(action: {
|
||||
if let scene = UIViewControllerRepresentableScene<UIViewController>(sceneID: "MyImmersiveScene") {
|
||||
openImmersiveSpace(scene)
|
||||
}
|
||||
}) {
|
||||
Text("Enter Immersive Space")
|
||||
}
|
||||
|
||||
Button(action: dismissImmersiveSpace) {
|
||||
Text("Dismiss Immersive Space")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Performance Metrics:
|
||||
* **Tokens/s:** 7.18
|
||||
* **Total Tokens:** 170
|
||||
* **Time to First Token:** 0.0s
|
||||
* **Stop Reason:** stop
|
||||
|
||||
---
|
||||
|
||||
## Test 10: Create a custom SwiftUI Layout container called `SimpleFlowLayout`.
|
||||
|
||||
*Source:* `test10.txt`
|
||||
|
||||
### Prompt:
|
||||
```swift
|
||||
Create a custom SwiftUI Layout container called `SimpleFlowLayout`.
|
||||
|
||||
This layout should arrange its subviews in a horizontal flow, starting a new row when the current row is full. It should behave similarly to a basic left-aligned flow layout in CSS.
|
||||
|
||||
The implementation must conform to the `Layout` protocol and implement both the `sizeThatFits(...)` and `placeSubviews(...)` methods.
|
||||
```
|
||||
|
||||
### Response:
|
||||
|
||||
To create a custom SwiftUI Layout container called `SimpleFlowLayout`, you can follow these steps:
|
||||
|
||||
1. Import the necessary modules:
|
||||
```swift
|
||||
import SwiftUI
|
||||
```
|
||||
2. Define the `SimpleFlowLayout` struct that conforms to the `Layout` protocol:
|
||||
```swift
|
||||
struct SimpleFlowLayout: Layout {
|
||||
var alignment: HorizontalAlignment = .start
|
||||
|
||||
func measure(in context: Context, configuration: NSDirectionalCombineShapeConfiguration) -> CGSize {
|
||||
let sizeThatFits = CGSize.zero
|
||||
|
||||
// Iterate through the subviews and calculate their sizes
|
||||
for view in self.subviews {
|
||||
let viewSize = view.layout(in: context, with: configuration).size()
|
||||
|
||||
if alignment == .start {
|
||||
sizeThatFits.width += viewSize.width
|
||||
} else if alignment == .center {
|
||||
sizeThatFits.width += viewSize.width * 2 - (viewSize.width / 2)
|
||||
} else if alignment == .end {
|
||||
sizeThatFits.width += viewSize.width
|
||||
}
|
||||
}
|
||||
|
||||
return sizeThatFits
|
||||
}
|
||||
|
||||
func layout(in nib: UIView, cgContext: CGContext, subviews: [UIView], @ViewChildren var indexSet) {
|
||||
let stack = UIStackView()
|
||||
stack.axis = .horizontal
|
||||
stack.alignment = alignment
|
||||
|
||||
// Add the subviews to the stack view
|
||||
for view in self.subviews {
|
||||
let swiftUIView = view.view(in: nib) as! UIView
|
||||
stack.addArrangedSubview(swiftUIView)
|
||||
}
|
||||
|
||||
// Set up constraints and layout the stack view
|
||||
var leftMargin = 0
|
||||
for index in indexSet {
|
||||
let swiftUIView = subviews[index].view(in: nib) as! UIView
|
||||
|
||||
if alignment == .start || alignment == .center {
|
||||
swiftUIView.translatesAutoresizingMaskIntoConstraints = false
|
||||
|
||||
// Set up constraints for the left edge of the view
|
||||
let xConstraint = NSLayoutConstraint(item: swiftUIView, attribute: .leading, relatedBy: .equal, toItem: nib, attribute: .leftMargin, multiplier: 1.0, constant: leftMargin)
|
||||
|
||||
// Set up constraints for the right edge of the view
|
||||
let xConstraint2 = NSLayoutConstraint(item: swiftUIView, attribute: .trailing, relatedBy: .equal, toItem: nib, attribute: .rightMargin, multiplier: 1.0, constant: -stack.frame.width)
|
||||
|
||||
// Add constraints to the stack view
|
||||
stack.addConstraints([xConstraint, xConstraint2])
|
||||
} else if alignment == .end {
|
||||
swiftUIView.translatesAutoresizingMaskIntoConstraints = false
|
||||
|
||||
// Set up constraints for the right edge of the view
|
||||
let xConstraint = NSLayoutConstraint(item: swiftUIView, attribute: .trailing, relatedBy: .equal, toItem: nib, attribute: .rightMargin, multiplier: 1.0, constant: -stack.frame.width)
|
||||
|
||||
// Set up constraints for the left edge of the view
|
||||
let xConstraint2 = NSLayoutConstraint(item: swiftUIView, attribute: .leading, relatedBy: .equal, toItem: nib, attribute: .rightMargin, multiplier: 1.0, constant: -leftMargin)
|
||||
|
||||
// Add constraints to the stack view
|
||||
stack.addConstraints([xConstraint, xConstraint2])
|
||||
}
|
||||
|
||||
leftMargin += swiftUIView.frame.width + (stack.spacing ?? 0)
|
||||
}
|
||||
|
||||
nib.addSubview(stack)
|
||||
}
|
||||
}
|
||||
```
|
||||
3. Use the `SimpleFlowLayout` in your SwiftUI views:
|
||||
```swift
|
||||
struct ContentView: View {
|
||||
var body: some View {
|
||||
VStack {
|
||||
Text("Row 1")
|
||||
.frame(width: 200, height: 50)
|
||||
|
||||
Text("Row 2")
|
||||
.frame(width: 200, height: 50)
|
||||
|
||||
Text("Row 3")
|
||||
.frame(width: 200, height: 50)
|
||||
}
|
||||
}.background(SimpleFlowLayout())
|
||||
}
|
||||
```
|
||||
In this example, the `ContentView` uses a `VStack` to vertically stack three rows of text views. The `SimpleFlowLayout` is applied as the background view for each row, arranging them in a horizontal flow with left alignment.
|
||||
|
||||
### Performance Metrics:
|
||||
* **Tokens/s:** 9.03
|
||||
* **Total Tokens:** 1073
|
||||
* **Time to First Token:** 0.0s
|
||||
* **Stop Reason:** stop
|
||||
|
||||
---
|
||||
|
||||
Reference in New Issue
Block a user