Body mass index Swift signal SIGABRT issues - swift

I'm new in programming. I wrote a code for a body mass index app, but I can't find why I have signal SIGABRT issues.
I didn't find my response or find a good tutorial on stackoverflow.
Can you tell me what is wrong with my code please ?
Thanks
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var lblHeight: UITextField!
#IBOutlet weak var lblWeight: UITextField!
#IBOutlet weak var txtFieldResult: UILabel!
var height = 0.00
var weight = 0.00
var result = 0.00
#IBAction func btnCalcul(sender: UIButton) {
height = (lblHeight.text as NSString).doubleValue
weight = (lblWeight.text as NSString).doubleValue
if height > 0.00 {
result = weight/(height*height)
txtFieldResult.text = "\(result)"}
else {txtFieldResult.text = "Eat please"}
}
}
App delegate info
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}

Related

Unity View is coming on top of current view

I am trying to integrate the Unity build for iOS into a pre-existing native app. I am using two buttons to start and stop the unity, but when I click on start, Unity view comes on the top of the current view and both the buttons disappear behind it. I used Unity 2019.1.11f1 for building the unity project.
Below is my App Delegate Code:
//
// AppDelegate.swift
// blue
//
// Created by Vikas Roy on 01/07/19.
// Copyright © 2019 MedleyOne. All rights reserved.
//
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var application: UIApplication?
#objc var currentUnityController: UnityAppController!
var isUnityRunning = false
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.application = application
unity_init(CommandLine.argc, CommandLine.unsafeArgv)
currentUnityController = UnityAppController()
currentUnityController.application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
if isUnityRunning {
currentUnityController.applicationWillResignActive(application)
}
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
if isUnityRunning {
currentUnityController.applicationDidEnterBackground(application)
}
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
if isUnityRunning {
currentUnityController.applicationWillEnterForeground(application)
}
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
if isUnityRunning {
currentUnityController.applicationDidBecomeActive(application)
}
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func startUnity() {
if !isUnityRunning
{
isUnityRunning = true
currentUnityController.applicationDidBecomeActive(application!)
}
}
func stopUnity() {
if isUnityRunning {
currentUnityController.applicationWillResignActive(application!)
isUnityRunning = false
}
}
}
Below is the View Controller Code:
//
// Unity3DViewController.swift
// blue
//
// Created by Vikas Roy on 25/07/19.
// Copyright © 2019 MedleyOne. All rights reserved.
//
import UIKit
class Unity3DViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func showUnitySubView() {
if let unityView = UnityGetGLView() {
view?.insertSubview(unityView, at: 0)
}
}
#IBAction func StartUnity(_ sender: Any) {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate
{
appDelegate.startUnity()
showUnitySubView()
}
}
#IBAction func StopUnity(_ sender: Any) {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate
{
appDelegate.stopUnity()
}
}
}
I want buttons to always stay on the top of the unity view. Any idea where the issue can be?
In ViewContoller, where Unity is loading,
create an outlet of your button
and call this function in viewDidload() this way:
let unityView = UnityGetGLView()
unityView.addsubView(yourbutton)

Iot Raspberry Pi Xcode - FIREBASE

I am connecting firebase to xcode. I keep getting a THREAD 1 : SIGBART ERROR. I'm new to xcode so I don't know what to do. Please help. It gives me the error at the part where it says **class AppDelegate: UIResponder, UIApplicationDelegate {*
//
// AppDelegate.swift
// H2KNOW
//
// Created by Sohil Bhatia on 9/4/18.
// Copyright © 2018 Sohil Bhatia. All rights reserved.
//
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
Please help me fix this. I really want to fix this. Thanks.

Swift Admob issue's Thread 1: EXC_BAD_INSTRUCTION

I've been working on the following problem for a couple of days now and I'm getting a little upset with it. So I have a project file https://github.com/PCmex/lift-side-memu-in-swift-3 and I successfully initiated Cocoapod with it. I followed the whole admob tutorial and did all the required steps. When I try to test the app the build is OK, but after the app launches it crashes and gives me the following information:
Screenshot for error message: Thread 1: EXC_BAD_INSTRUCTION
The log gives me the following information:
Google Mobile Ads SDK version:(GADRequest.sdkVersion())
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
Here is the app delegate.swift
import UIKit
import GoogleMobileAds
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//Use Firebase library to configure APIs
FirebaseApp.configure()
GADMobileAds.configure(withApplicationID: "ca-app-pub-***")
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
And this is the ViewController.swift
import UIKit
import GoogleMobileAds
import Firebase
class ViewController: UIViewController {
#IBOutlet weak var btnMenuButton: UIBarButtonItem!
#IBOutlet weak var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
// Aanroepen print functie voor de Google AdMob banner
print("Google Mobile Ads SDK version:(GADRequest.sdkVersion())")
bannerView.adUnitID = "ca-app-pub-***"
bannerView.rootViewController = self
bannerView.load(GADRequest())
// Do any additional setup after loading the view, typically from a nib.
if revealViewController() != nil {
// revealViewController().rearViewRevealWidth = 62
btnMenuButton.target = revealViewController()
btnMenuButton.action = #selector(SWRevealViewController.revealToggle(_:))
// revealViewController().rightViewRevealWidth = 150
// extraButton.target = revealViewController()
// extraButton.action = "rightRevealToggle:"
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I'm pretty sure I've been installing cocoa-pod / AdMob and all prerequisites correctly. When I do all steps in a new project everything works fine. But I'm trying to understand why it doesn't work in my current project. Hope someone could point me in the right direction, thanks in advance!
The variable bannerView is an implicitly unwrapped optional. That means that it is an optional variable type. Remember that unwrapping optionals will crash if its value is nil, so normally you would do some optional chaining like if let to test before unwrapping to prevent crashing. In your case, bannerView is nil, so your application crashed. An implicitly unwrapped optional is declared by placing a ! after its type (in your case, GADBannerView!).
I suggest you to go to the storyboard (or XIB) of your controller, select your GADBannerView and go to the connections inspector
And check if there is anything in the "Referencing Outlets" section (except for "New Referencing Outlet). If there is, break the connection by clicking the X button.
Then delete the #IBOutlet weak var bannerView line in the Controller and reconnect the GADBannerView to ViewController. If there is nothing in the section, simply delete #IBOutlet weak var bannerView and reconnect the GADBannerView to ViewController

Change the background color of “More” UINavigationController in UITabBar in swift

I would to change the background color of the "More" UINavigationController in my UITabBar. When I try to change the background color, it's not a good result. That is my code:
import UIKit
import SwiftyJSON
import FontAwesomeKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Sound, .Badge], categories: nil))
// Override point for customization after application launch.
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.whiteColor()], forState: .Selected)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.whiteColor()], forState: .Normal)
tabBarController.moreNavigationController.navigationBar.barStyle = .Default
tabBarController.moreNavigationController.navigationBar.backgroundColor = UIColor.orangeColor()
tabBarController.moreNavigationController.navigationBar.tintColor = UIColor.whiteColor()
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
And this is the result:
Capture of UINavigationController
Change the barStyle to UIBarStyle.Black this will create a solid background and do also add
tabBarController.moreNavigationController.navigationBar.translucent = false
This should do the work for you.

Why doesn't my best score value load after game closes and launches again?

My best score value works perfectly fine if the game keeps running, but it won't load the best score if the game closed and was to launch again. It goes back to zero every time it the launches again.
Here is what I'm doing:
import SpriteKit
class EM: SKScene, SKPhysicsContactDelegate {
var bestScoreText = SKSpriteNode()
let bestScoreCategory: UInt32 = 1 << 4
var bestScoreLabelNode = SKLabelNode()
var bestScore = NSInteger()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
var bestScore = 0
var bestScoreDefault = NSUserDefaults.standardUserDefaults()
bestScore = NSUserDefaults.standardUserDefaults().integerForKey("bestScore")
}
func didBeginContact(contact: SKPhysicsContact) {
if (score > bestScore) {
bestScore = score
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(bestScore, forKey: "bestScore")
defaults.synchronize()
}
} else {
......
}
AppDelegate.Swift:
class AppDelegate: UIResponder, UIApplicationDelegate {
let defaults = NSUserDefaults.standardUserDefaults()
let defaultValues = ["bestScore" : 0]
defaults.registerDefaults(defaultValues)
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
First of all, don't use an optional value, the score is supposed to be never nil
The usual way to treat user defaults is to register the key value pairs to have reliable default values.
So in AppDelegate – as soon as possible – write
let defaults = NSUserDefaults.standardUserDefaults()
let defaultValues = ["bestScore" : 0]
defaults.registerDefaults(defaultValues)
It doesn't matter that this code is called at every application launch.
If any of the keys exist in the user defaults database, the default value is ignored.
Declare the variable bestScore as non-optional Int with a starting value of 0.
As Int is the default type, the type can be omitted.
var bestScore = 0
To read the high score from user defaults use
bestScore = NSUserDefaults.standardUserDefaults().integerForKey("bestScore")
Checking for nil is not needed as we know we have a default non-optional value of 0
To write the high score to user defaults use
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(bestScore, forKey: "bestScore")
defaults.synchronize()