The use of autoreleasepool{} in swift is as follows.
autoreleasepool {
// all usage here
}
But I do not know how to use it in public.
Example
class ViewController: UIViewController {
autoreleasepool {
var results: Results<Item>!
}
override func viewDidLoad() {
super.viewDidLoad()
.....
}
}
If this is public, an error occurs. Does autoreleasepool{} need to be in func only?
Related
Consider the following code:
class ViewController: UIViewController {
var dummy = DummyClass()
override func viewDidLoad() {
super.viewDidLoad()
var copyDummy = dummy
if (copyDummy === dummy){
print("Same instance")
}
increment(©Dummy)
}
func increment(_ number: inout DummyClass) {
number.change += dummy.change
}
}
class DummyClass {
var change = 10
}
Now from my understanding, both copyDummy and dummy point to the same memory location.
When I call func increment using increment(&dummy) there is a runtime error since I am trying to write and read the same memory location simultaneously.
But when I callincrement(©Dummy) there is no error.
Why such behaviour if copyDummy is also accessing the same memory location as dummy variable?
Sorry if this turns to be a very basic question and/or if I have got my understanding incorrect. I am trying to wrap my head around this from quite some time.
Thanks in advance!
Modified code as per helpful suggestions:
class ViewController: UIViewController {
var dummy = DummyClass()
override func viewDidLoad() {
super.viewDidLoad()
increment(dummy)
}
func increment(_ number: DummyClass) {
number.change += dummy.change
}
}
class DummyClass {
var change = 10
}
Isnt func increment writing the same memory location ie. numbers.change from where its also reading ie. dummy.change
I have a singleton to store some global data for my macOS app, one of my ViewController keeps modifying data. I want to simultaneously show the changes in a View, which is related to another ViewController. what 's the best way to do this?
Global Data:
final class AppData {
static var logs: [LogData] = []
}
ViewController 1:
class FirstViewController: NSViewController {
AppData.logs.append(newLogData)
}
ViewController 2:
class SecondViewController: NSViewController {
// what's the best way to simultaneously watch the change of AppData.logs?
}
If your App is planned to be macOS only you can use a NSObjectController. This is definitively the easiest approach and you can do most of the configuration in Interface builder. It works internally with bindings. In case of an array you want to observe, you would use a NSArrayController.
One way is to use the notificationcenter
In viewcontroller2 add:
override func viewDidLoad() {
super.viewDidLoad()
notificationCenter.default.addObserver(self,
selector: #selector(view1DidChange),
name: "view1DidChange",
object: nil
)
}
#objc private func view1DidChange(_ notification: Notification) {
// Do something
}
In viewcontroller1 add
notificationCenter.default.post(name: "view1DidChange", object: self)
This can be repeated in every class, that should listen.
Here i am sharing the Delegate & Protocol approach to achieve this functionality.
final class AppData {
static var logs: [LogData] = []
}
protocol FirstViewControllerDelegate {
func ViewControllerDelegate(appData:[LogData])
}
class FirstViewController: NSViewController {
var delegate:FirstViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
AppData.logs.append(newLogData)
self. delegate?.ViewControllerDelegate(appData: AppData.logs)
}
}
class SecondViewController: NSViewController,FirstViewControllerDelegate {
var firstViewController:FirstViewController = FirstViewController()
override func viewDidLoad() {
self.firstViewController.delegate = self
}
func ViewControllerDelegate(appData:[LogData]){
//Do Update the UI
}
}
I've looked all over google's AdMob site, I just want a list of all the inbuilt functions I can use for banners and interstitials.
eg. adViewDidReceiveAd() interstitialDidDismissScreen()
If anyone knows where I can find this, please let me know.
This is bannerView functions where you have to implement GADBannerViewDelegate to be able to use it (don't forget to set the delegate for your banner as myBanner.delegate = self so they will be called).
func adViewDidReceiveAd(bannerView: GADBannerView!) {
}
func adViewDidDismissScreen(bannerView: GADBannerView!) {
}
func adViewWillDismissScreen(bannerView: GADBannerView!) {
}
func adViewWillPresentScreen(bannerView: GADBannerView!) {
}
func adViewWillLeaveApplication(bannerView: GADBannerView!) {
}
func adView(bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
}
And for interstitial delegates you will need to implement GADInterstitialDelegate to able to use it.(don't forget to set the delegate for your interstitlal as interstitial.delegate = self so they will be called).
func interstitialDidReceiveAd(ad: GADInterstitial!) {
}
func interstitialDidDismissScreen(ad: GADInterstitial!) {
}
func interstitialWillDismissScreen(ad: GADInterstitial!) {
}
func interstitialWillPresentScreen(ad: GADInterstitial!) {
}
func interstitialWillLeaveApplication(ad: GADInterstitial!) {
}
func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
}
Implement these after class declaration, ex: class test:GADInterstitialDelegate {}
I want to touch a button in ViewController and run a simple function in a custom class. No parameters needed. Code is basically:
class MyClass: UIView {
//init function is here
//other setup stuff
...
func SetFocus() {
//I want to set the camera focus to infinity.
var error: NSErrorPointer = nil
var pos: CFloat = 1.0
captureDevice!.lockForConfiguration(error)
captureDevice!.setFocusModeLockedWithLensPosition(pos, completionHandler: nil)
captureDevice!.unlockForConfiguration()
}
}
and in ViewController
import UIKit
class ViewController: UIViewController {
#IBOutlet var myClass: MyClass!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func myButton(sender: UIButton) {
myClass.SetFocus()
}
}
I've tried everything I can think of (new at Swift) and the app either crashes or the function I'm calling doesn't run. Everything else in MyClass works OK.
Try to delete the #IBOutlet from before MyClass.
var myClass : MyClass!
You can also try this:
var myClass : MyClass! = MyClass()
From the looks of it, however, everything you are doing in setFocus() in MyClass, can easily be recreated in the ViewController.
Try this
#IBAction func myButton(sender: UIButton) {
var ClassViewController = self.storyboard.instantiateViewControllerWithIdentifier("StoryboardID") as! UIViewController
ClassViewController.MyFunction()
}
I am attempting to create an app in Swift using a Storyboard. I want to have a button trigger the UIProgressView's animation.
*Expected '{' in body of function declaration*
import Cocoa
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
#IBAction func PressKit(sender: AnyObject) {
func startAnimating()
}
}
If you are trying to call the function "startAnimating", then don't use the "func" keyword in front of it, just use "startAnimating()" to call the function. You will then need to define the function (outside of the "PressKit" function). You might want to do this somewhere else in the class using, say:
func startAnimating() {
...some code...
}