I do not understand why AVPlayer is always nil
Unexpectedly found nil while unwrapping an Optional value
struct SatelliteVideoView: View {
init() {
player = AVPlayer(url: URL(string: "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4")!)
}
var body: some View {
VideoPlayer(player: player!)
}
The documentation uses this code.
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hi all. I need help with this as i struggled to actually use the objective c library.
https://github.com/ngageoint/simple-features-geojson-ios
It says install from cocoapods. check. I got it.
My issue is with the bridging header. I do not understand the posts and answers that I came across in stackoverflow.
do i not need to install the library from cocoapods? instead copying the .h and .m to my swift project? And thereby generating that bridging header file?
So i did just that and set
#import "sf-geojson-ios-Bridging-Header.h"
in the bridging header file. But when i build it, i keep getting the error message
Build input file cannot be found: project-Bridging-Header.h'. Did you forget to declare this file as an output of a script phase or custom build rule which produces it?
what is the correct way to do this? am i missing something?
Anyone experienced this.
My goal is to use AppStorage to save the value in the stepper but the behavior is whaked. Buttons get disabled even when theyre not at both ends of the array
if i use State, it works a bit better but the buttons only get disabled after the 2nd or 3rd try when the index is either 0 or at the end of the array.
@AppStorage("index") private var opacity = 0
let options = [40, 50, 70, 80, 90, 100]
Stepper {
Text("\("Opacity") \(options[opacity].description)%")
} onIncrement: {
opacity = opacity + 1
if opacity >= opacity.count {
opacity = opacity.count - 1
}
print("onincrement="+String(opacity))
} onDecrement: {
opacity = opacity - 1
if opacity < 0 {
opacity = 0
}
print("ondecrement="+String(opacity))
}
The sample code looks simple. I am not sure what else I could be missing?
Hi. I am not sure how to do this case scenario I am having.
Please see sample code
@State private var tileLayers: [GMSURLTileLayer]?
@State private var isShowingLegend = true
var body: some View {
MapView(
tileLayers: tileLayers
)
.task {
setupMenuItems()
if load {
load = false
processRainWatcher()
}
}
.overlay(
isShowingLegend ?
HStack {
RainWatcherLegendView()
Spacer()
}
.transition(.move(edge: isShowingLegend ? .leading : .trailing))
: nil,
alignment: .bottom
)
}
This is not a working code. But i wish to ask suggestion. because the overlay shows a legend view. If isShowingLegend.toggle() is triggered, it animates sliding in and out of the screen.
Problem mis, everything gets rebuilt. so the tile layer in the MapView() gets re-rendered again.
is there a way not to let this happen, but still let the legend animate sliding in and out?
or perhaps pass a binding variable to the legend view? and do the animation in that view? so that the parent view does not get rebuilt?
thoughts?
Hi. I am having problems with animating my view sliding in and out and hiding if not in view.
The thing is, fading in and out works. but moving it does not. What could be wrong or what Am i missing?
import SwiftUI
struct MyView: View {
@Binding var isShowing: Bool
var body: some View {
VStack(spacing: 0) {
Group {
row("#ff0000", "color1")
row("#ff0000", "color2")
row("#ff0000", "color3")
row("#ff0000", "color4")
row("#ff0000", "color5")
row("#ff0000", "color6")
}
Group {
row("#ff0000", "color7")
row("#ff0000", "color8")
row("#ff0000", "color9")
row("#ff0000", "color10")
row("#ff0000", "color11")
row("#ff0000", "color12")
}
}
.frame(maxWidth: 130, maxHeight: 250)
.background(.white.opacity(0.7))
.padding()
// .transition(.move(edge: isShowing ? .leading : .trailing))
.opacity(isShowing ? 1 : 0)
.animation(isShowing ? .easeIn(duration: 0.7) : .easeOut(duration: 0.7), value: isShowing)
}
private func row(_ color: String, _ labelKey: String) -> some View {
return HStack {
Rectangle()
.fill(Color(UIColor(hex: color)))
.frame(width: 15, height: 15)
.padding(.leading, 3)
Text(labelKey.uppercased())
.foregroundColor(.black)
Spacer()
}
}
}
and this is how to use the view
var body: some View {
@State private var isShowing = true
HStack {
Button("click me", action: {
isShowing.toggle()
})
.padding()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.overlay(
MyView(isShowing: $isShowing),
alignment: .bottom
)
}
Hi, this is my custom view
struct MyView: View {
var body: some View {
GeometryReader { geometryReader in
ZStack {
VStack(spacing: 0) {
Rectangle()
.fill(.red)
.frame(height: geometryReader.size.height * 0.25)
Rectangle()
.fill(.orange)
.frame(height: geometryReader.size.height * 0.5)
Rectangle()
.fill(.yellow)
.frame(height: geometryReader.size.height * 0.25)
}
VStack {
Spacer()
Image(systemName: "figure.wave")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(height: geometryReader.size.height * 0.8)
}
}
}
.frame(maxWidth: 100, maxHeight: 200)
.background(.white.opacity(0.7))
.cornerRadius(5)
.shadow(color: Color.gray.opacity(0.7), radius: 8, x: 0, y: 0)
.padding()
}
}
struct MyView_Previews: PreviewProvider {
static var previews: some View {
MyView()
}
}
I am not sure what the best approach for this is. I want to place some text (please see image).
I was thinking adding another VStack at the bottom but i am not sure how i can position it correctly.
Thoughts?
I have an image and text side by side in an HStack set to top alignment.
I have no clue what this extra space is. The square shape and the text are not top aligned.
I tried to set padding(0) to Text() to no avail.
Any idea what this is?
This is the code for the HStack.
HStack(alignment: .top) {
Rectangle()
.fill(.red)
.frame(width: 15, height: 15)
.padding(.leading, 3)
Text("Heyasfd asdfsafds af awf s".uppercased())
.foregroundColor(.black)
Spacer()
}
Thoughts?
Hi. I am using Kingfisher library to load images. My problem is that when i resize the width, the calculated height does not follow. the height of the image when displayed is still short.
What could possibly be wrong in my code?
import SwiftUI
import Kingfisher
struct MyView: View {
var geometryProxy: GeometryProxy
var body: some View {
HStack {
HStack {
KFImage.url(URL(string: "https://volcano.si.edu/gallery/photos/GVP-11076.jpg"))
.cancelOnDisappear(true)
.fade(duration: 0.25)
.resizable()
.scaledToFit()
}
.frame(maxWidth: geometryProxy.size.width * 0.4, maxHeight: getHeight(Float(geometryProxy.size.width) * 0.4, Float(geometryProxy.size.height), Float(geometryProxy.size.width)))
Text("Text 1 Label")
.frame(maxWidth: geometryProxy.size.width * 0.65, alignment: .topLeading)
.padding(.leading, 10)
.padding(.top, 3)
Image(systemName: "chevron.right")
.frame(maxWidth: geometryProxy.size.width * 0.05)
.padding(.leading, 0)
}
.contentShape(Rectangle())
}
private func getHeight(_ imageWidth: Float, _ imageHeight: Float, _ screenWidth: Float) -> CGFloat {
// let ratio = Float(placeholder!.size.height / placeholder!.size.width)
// print(ratio)
// print(ratio * width)
// print("______")
// return CGFloat(ratio * width)
let ratio = screenWidth / imageWidth
return CGFloat(imageHeight * ratio)
}
}
The Image should expand since i set the width of the image to at least 40% of the screen width.
Or could my calculation for getting the height may be wrong? Thougts?
Hi. this is my observable object
final class ToolbarItemProperties: ObservableObject {
var visible = false
var disabled = false
var icon = "questionmark"
var action: () -> Void = { }
var opacity: Double = 1
}
the opacity property value should either be 0.5 or 1 depending on the value of the property disabled. How to listen to it?
The purpose for this is because if i set a button to disabled, it does not render any way to the user to let them know it is disabled. So i thought of changing the opacity instead, hence the opacity property.
So if i do something like this
item.disabled = true
the opacity property value should become 0.5. if disabled is set to false, the opacity value will be 1.
I want to have a property in a struct/class where it is of type custom View
e.g.
var view: some View?
So many issues here, I ended up doing
var view: AnyView?
But i got stuck here because whenever I assign a value to this, the app crashes. saying
Could not cast value of type '__NSCFNumber' (0x7fdcdf9c3480) to 'NSString' (0x1192bac50).
This i do not understand
view should be some view so it si neither number nor string. any idea why?
also. if i do this
var view: (some View)?
it is ok. but when i set a value to that property it says Cannot assign value of type 'DailyCityReportView' to type 'some View'
My goal is to pass a custom view to a property.
I am not really sure what is the cause but this happens if i overwrite values. if i clear the app from scratch and store them all it works ok. Thoughts?
my exact message is Attempting to store >= 4194304 bytes of data in CFPreferences/NSUserDefaults on this platform is invalid
Description of keys being set:
holocene_update: string value, approximate encoded size: 25
Description of keys already present:
holocene: string value, approximate encoded size: 5216090
GMSMapsUserClientZwiebackCookie: string value, approximate encoded size: 175
GMSMapsUserCookie: data value, size: 48
kGMSServerVersionMetadataTrackerSavedServerVersionMetadataKey: data value, size: 30
holocene_update: string value, approximate encoded size: 25
com.google.Maps.GMSCoreLastVersion: string value, approximate encoded size: 6
cyclone_alert_last: string value, approximate encoded size: 5
com.google.Maps.GMSSDKLastVersion: string value, approximate encoded size: 5
GMSMapsUserClientCohort: string value, approximate encoded size: 4
kGMSMapsUserClientLegalCountry: string value, approximate encoded size: 2
...
Total keys: 12 - Average approximate value size: 434699 bytes
2022-12-19 17:49:08.777899+0800 PH Weather And Earthquake Updates[31305:4102377] [User Defaults] CFPrefsPlistSource<0x600002b80980> (Domain: ************, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null), Contents Need Refresh: No): Transitioning into direct mode
Is there a way to increase this limit? I need to store data.
I have a function like this
@ViewBuilder
func addInfo(_ text: String, action: (() -> Void)?, addDivider: Bool = true, centerAlignment: Bool = false, isBold: Bool = false) -> some View {
VStack {
Text(text)
.fontWeight(isBold ? .bold : .regular)
.frame(maxWidth: .infinity, alignment: centerAlignment ? .center : .leading)
.padding([.leading, .trailing], 10)
.padding([.top, .bottom], 5)
.contentShape(Rectangle())
.onTapGesture {
if let action {
action()
}
}
if addDivider {
Divider()
}
}
}
I do not understand why i get a compile error
Expected type after '->'
if i call the function like this
addInfo("Sample", action: () -> Void {
})
The error points to Void. What is the correct way to do this? Please advise.
Hi. I am having issues with json parsing. all my source data use double quote except for one.
here is the sample
do {
let json = "[['16772', 'Cebu', 'Philippines']]"
if let jsonArray = try JSONSerialization.jsonObject(with: Data(json.utf8), options: []) as? [[Any]] {
print("Hello World")
}
} catch {
print(error)
}
if the json string is
let json = "[[\"16772\", \"Cebu\", \"Philippines\"]]"
it works ok. but if it is single quote, then it gives out the error message
Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around line 1, column 2." UserInfo={NSDebugDescription=Invalid value around line 1, column 2., NSJSONSerializationErrorIndex=2}
What am i missing to enable parsing single quote normally? Thoughts?
Hi. I have no clue what I am doing wrong or am i missing something else. I keep getting the error message
CoreData: error: Failed to load model named DMTideCity
Please see attached image for data model. My goal is just to load this but I dont know why I keep getting that error message.
struct TideDataController {
static let shared = TideDataController()
let cityContainer: NSPersistentContainer
init() {
print("init")
cityContainer = NSPersistentContainer(name: "DMTideCity")
print(cityContainer)
cityContainer.loadPersistentStores { description, error in
if let error {
print("Core Data Citys failed to load: \(error.localizedDescription)")
}
else {
print("Core Data Citys loaded")
}
}
}
}
In my view, i declare a variable like this
let tideDataController = TideDataController.shared
So it never reaches the print(cityContainer) because of that error message.
Thoughts?
This is what I tried to do. I am not sure what is wrong. anyone got ideas?
let data: Data? = try? JSONSerialization.data(withJSONObject: "")
and it says
reason: '*** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write'
even if i put in "{}" error is the same. thoughts?
Purpose for this is i want set this to a URLRequest.httpBody
Thoughts?