In Swift Playground 4.6.2 the package PlaygroundSupport is no longer available to Playground apps. The following test previously permitted apps run in the Playground vs compiled in XCode to support different behavior:
#if canImport(PlaygroundSupport)
container = NSPersistentContainer(name: "myApp", managedObjectModel: Self.createModel())
#else
container = NSPersistentCloudKitContainer(name: "myApp")
#endif
Since Swift Playground 4.6.2 the PlaygroundSupport package is no longer available for app projects in Playgrounds.
Is there a different compile type test which can be used to differentiate compilation for Swift Playground apps ?
I am currently having to use a runtime workaround (below) but would prefer a compile time test is an alternative is available.
public static var inPlayground: Bool {
if Bundle.allBundles.contains(where: { ($0.bundleIdentifier ?? "").contains("swift-playgrounds") }) {
return true
} else {
return false
}
}
Post
Replies
Boosts
Views
Activity
On macOS 13.0 (M1 Mac Book Pro), downloaded Playgrounds 4.2, and selected one of the new app playgrounds e.g getting started with Apps, or just a simple new app. A list of errors is immediately produced, and no preview can be seen:
What could be causing this ?
I have been struggling to get a functional UISearchController in a SwiftUI app, under a tab bar. I've scoured the web and documentation, and managed to get something almost there, however the keyboard on the search view does not respond to left and right controls. I've seen a post on StackExchange where someone has a similar issue, but no solution.
Can anyone help to show how to make a functional search view in a SwiftUI app on tvOS 13 ?
One thing to note, while working through the views, I discovered a bug in USearchView which doesn't set a horizontal constraint for the keyboard area in one of its' inner views. I include a fix for that which adds in the constraint if one can't be found.
I've attached the full source, just add to an empty tvOS SwiftUI project (and remove AppDelegate and ContentView)
TestSearch.swift - https://developer.apple.com/forums/content/attachment/af387dcd-4501-44b3-90ac-f71177634103
The UISearchView HostingView:
struct PageView<Page: View>: View {
var viewController: UIHostingController<Page>
@ObservedObject var state: SearchState
init(_ resultsView: Page, state: SearchState) {
self.viewController = UIHostingController(rootView: resultsView)
self.state = state
}
var body: some View {
PageViewController(controller: viewController, state: state)
}
}
struct PageViewController: UIViewControllerRepresentable {
var controller: UIViewController
@ObservedObject var state: SearchState
func makeUIViewController(context: Context) -> UINavigationController {
let searchController = UISearchController(searchResultsController: controller)
searchController.searchResultsUpdater = state
searchController.searchBar.placeholder = NSLocalizedString("Enter search (e.g. Shawshank)", comment: "")
let searchContainer = UISearchContainerViewController(searchController: searchController)
searchContainer.title = NSLocalizedString("Search", comment: "")
let searchNavigationController = UINavigationController(rootViewController: searchContainer)
return searchNavigationController
}
func updateUIViewController(_ searchContainer: UINavigationController, context: Context) {
if searchContainer.children.count > 0,
let searchController = (searchContainer.children[0] as? UISearchContainerViewController)?.searchController,
let searchControllerView = searchController.view,
searchController.children.count > 1,
let uiKBFocusView = searchController.children[1].view,
!searchControllerView.constraints.reduce(false, {$0 || ($1.firstAttribute == NSLayoutConstraint.Attribute.centerX) }){
let horizontalConstraint = NSLayoutConstraint(item: uiKBFocusView, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: NSLayoutConstraint.Relation.equal, toItem: searchControllerView, attribute: NSLayoutConstraint.Attribute.centerX, multiplier: 1, constant: 0)
searchControllerView.addConstraints([horizontalConstraint])
}
}
}
The Search State and results view
class SearchState: NSObject, ObservableObject, UISearchResultsUpdating {
@Published var text: String = ""
func updateSearchResults(for searchController: UISearchController) {
let searchBar = searchController.searchBar
self.text = searchBar.text ?? ""
}
}