Posts

Post not yet marked as solved
3 Replies
2.9k Views
Hi Apple Dev Community,My app binary recently got rejected in the app review. The reason for this being "my app includes HealthKit but never uses it".However, confusing is HealthKit either doesn't exist or is toggleed "off" for my app IDs, Xcode project and info.plist. Please take a look at below:App IDs: https://ibb.co/fCHhkG0Xcode: https://ibb.co/bN8Qjw6info.plist: https://ibb.co/f2jRgTrAs you can see, HealthKit is turned off for both App IDs and my Xcode project, while there is no entry for it in my info.plist...Apparenly, according to my app review correspondant, my info.plist "contains definitions for NSHealthShareUsageDescription and NSHealthUpdateUsageDescription values". I find this weird as my info.plist contains no such definitions.Does anybody know where exactly these values are stored and how to remove them?Any help is greatly appreciated. Thanks in advance! 🙂
Posted
by kc_dev.
Last updated
.
Post not yet marked as solved
0 Replies
282 Views
I am trying to display text in a UILabel vertically after the device changes to landscape from portrait.I've managed to (forcefully) stack them vertically by changing its frame size and setting numberOfLines = 0.This sort of produces the result I want, except I get white spaces between each character after rotation, elongating the word greatly.Please check the link below to get a better idea of what I am experiencing.ttps:(Double Slash)ibb[dot]co{SLASH}6g8SHt9Ideally, there would be no unnecessary white spaces after ratation, and it would be nice and compact like it is in Portrait.Does anyone know how I can achive this? Below is my code:import UIKit class ViewController: UIViewController { var _W = CGFloat() var _H = CGFloat() var wordLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() _W = UIApplication.shared.statusBarOrientation.isPortrait ? view.frame.width : view.frame.height _H = UIApplication.shared.statusBarOrientation.isPortrait ? view.frame.height : view.frame.width wordLabel = UILabel() wordLabel.text = "Text" view.addSubview(wordLabel) formatLabel(label: wordLabel, fontSize: 64, color: UIColor.magenta, tag: 1) } func formatLabel(label: UILabel, fontSize: CGFloat, color: UIColor, tag: Int) { label.font = UIFont.boldSystemFont(ofSize: fontSize) label.textColor = color label.tag = tag label.textAlignment = .center label.adjustsFontSizeToFitWidth = true label.numberOfLines = 0 positionView(label, isPortrait: UIApplication.shared.statusBarOrientation.isPortrait) } override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) { positionView(wordLabel, isPortrait: !UIApplication.shared.statusBarOrientation.isPortrait) } func positionView(_ view: UIView, isPortrait: Bool) { if (view.tag == 1) { if (isPortrait) { wordLabel.frame = CGRect(x: 0, y: 0, width: _W, height: 80) wordLabel.center = CGPoint(x: _W/2, y: (_H - _W)/4) } else { wordLabel.frame = CGRect(x: 0, y: 0, width: 50, height: _W) wordLabel.center = CGPoint(x: (_H - _W)/4, y: _W/2) } } } }
Posted
by kc_dev.
Last updated
.
Post not yet marked as solved
1 Replies
358 Views
Hi Apple Dev Community,Is there an easy way to select a SFX or music to play for a specific audio player instance in Swift?You see, in pretty much every other language I have learned, you can do something likeaudioPlayer.play("mySoundEffect.wav"); audioPlayer.play("anotherSoundEffect.wav");where the argument for .play( ) is simply the URL or the file name of the audio file.However, no matter how many articles I read or tutorials I watch, no where can I find an equivalent for Swift...In Swift, you instantiate an audio player by callingaudioPlayer = try AVAudioPlayer(contentsOf: URL.init(Bundle.main.path(forResource: "mySoundEffect", ofType: "wav")!))and then play the audio by callingaudioPlayer.play()with no arguments.Does this mean that, if I have multiple sound effects I want to sample from, I have to create an audio player instance for each one of them?What if I have over 100+ sound effects in my game app? Will I then have to create 100+ instances of audio player?
Posted
by kc_dev.
Last updated
.
Post marked as solved
2 Replies
1.4k Views
Hi Apple Dev Community,I have a simple question regarding the usage/implementation of String within a UILabel.So basically, I want to store and display a message in a Label. In my case, I want it to say something like:"3 Gems Remaining!"However, I want the "3" portion of the string to have a larger size than the rest of the string, and a different color (and possibly a different font family, too).Is it possible to do this? Or will I have to create 2 separate Labels, one containing the number of gems remaining, and another for the rest of the message?Thanks in advance! 🙂KC
Posted
by kc_dev.
Last updated
.
Post not yet marked as solved
8 Replies
2.5k Views
Hi Apple Dev Community,I have a single ViewController, in which I have a (custom) CollectionView, in which I have 64 (custom) CollectionViewCells, each in which I have a single UILabel and UIImageView.My goal is to populate the CollectionView with CollectionViewCells, with a common background image and a label (in this example, the label is simple the indexPath.item)Straightfoward enough....or you would think. I'm encountering weird behavior where only the first cell gets a label! To make matters worse, none of the cells are getting an image!I figured the background color might be the culprint, so I commented the line out....and all of a sudden, the 8x8 grid turns into a 4x4 grid!WHAT THE **** IS GOING ON!?? I've never seen any behavior like this....below is the code:ViewController.swiftimport UIKit class ViewController: UIViewController{ var collectionView: collectionView! override func viewDidLoad() { super.viewDidLoad() collectionView = newCollectionView() } func newCollectionView() -> CollectionView { let collectionView = CollectionView(viewController: self) self.view.addSubview(collectionView) collectionView.activateConstraints() return collectionView } }CollectionView.swiftimport UIKit class CollectionView: UICollectionView, UICollectionViewDelegate, UICollectionViewDataSource { var viewController: ViewController! init(viewController: ViewController) { self.viewController = viewController let width = viewController.view.frame.width - 20 let frame = CGRect(x: 0, y: (viewController.view.frame.height - width)/2, width: width, height: width) let flowLayout = FlowLayout() super.init(frame: frame, collectionViewLayout: flowLayout) self.delegate = self self.dataSource = self self.register(CollectionViewCell.self, forCellWithReuseIdentifier: "Cell") self.backgroundColor = UIColor.red } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } func activateConstraints() { let view = viewController.view! translatesAutoresizingMaskIntoConstraints = false centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true widthAnchor.constraint(equalToConstant: view.bounds.width).isActive = true heightAnchor.constraint(equalToConstant: view.bounds.width).isActive = true contentInsetAdjustmentBehavior = .always } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 64 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) as! CollectionViewCell cell.label.text = "\(indexPath.item)" cell.backgroundColor = UIColor.gray // Comment this line out and the 8x8 grid turns into a 4x4 grid! return cell } }FlowLayout.swiftimport UIKit class FlowLayout: UICollectionViewFlowLayout { var gridSize: Int = 8 override init() { super.init() self.minimumInteritemSpacing = 1 self.minimumLineSpacing = 1 self.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 0, right: 10) } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override func prepare() { super.prepare() // Make sure the collection view is valid guard let collectionView = collectionView else { return } let marginsAndInsets = sectionInset.left + sectionInset.right + collectionView.safeAreaInsets.left + collectionView.safeAreaInsets.right + minimumInteritemSpacing * CGFloat(gridSize - 1) // Make item width as large as possible without bleeding let cellSize = ((collectionView.bounds.size.width - marginsAndInsets) / CGFloat(gridSize)).rounded(.down) itemSize = CGSize(width: cellSize, height: cellSize) } override func invalidationContext(forBoundsChange newBounds: CGRect) -> UICollectionViewLayoutInvalidationContext { let context = super.invalidationContext(forBoundsChange: newBounds) as! UICollectionViewFlowLayoutInvalidationContext context.invalidateFlowLayoutDelegateMetrics = newBounds.size != collectionView?.bounds.size return context } }
Posted
by kc_dev.
Last updated
.
Post marked as solved
1 Replies
2.2k Views
Hi Apple Dev Community,I've searched both here and google for similar questions, but none of the answers I found actually helped me...So bacically, I am trying to add subviews (in my case UICollectionView) programmatically, but when I do so, the delegate methods never get called.Here is the code:import UIKit class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { var collectionView: UICollectionView! override func viewDidLoad() { super.viewDidLoad() let myLayout = UICollectionViewFlowLayout() collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: myLayout) collectionView.backgroundColor = UIColor.red collectionView.delegate = self view.addSubview(collectionView) print("viewDidLoad") } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { print("numberOfItemsInSection") return 50 } // Initialize each cell func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { print("cellForItemAt") let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) myCell.backgroundColor = UIColor.blue return myCell } }As you can see, viewDidLoad is successfully called, but the deledate methods are never called.Please notice how I explicitly setcollectionView.delegate = selfHowever, even that is not enough to get the delegate methods to fire. Clearly, I must be missing something.Does anyone know how I can fix this code?Thanks in advance!KC
Posted
by kc_dev.
Last updated
.
Post not yet marked as solved
8 Replies
1.6k Views
Hi Apple Dev Community,I'm moving along with my project, whose hierarchy is getting more comlicated by day.At this point, it has become necessary to programmatically access the top level view controller from within a child view.My first approach was to create a custum init( ) method for the child view, and then pass the view controller as a parameter in the said init( ) method.However, my research suggests that that is a bad programming practice. I have found many articles that show how to programmatically add a child view controller to a parent view controller, but could not find how to add a child view to a parent view controller.Again, I need a valid reference to the parent view controller from within the child view that doesn't cause the program to explode.Does anyone know how to achieve this?Many thanks in advance! 🙂KC
Posted
by kc_dev.
Last updated
.
Post marked as solved
3 Replies
3.4k Views
Hi Apple Dev Community,I am rather new to Swift. So please bare with me if the following question sounds dumb..I don't understand why I am getting an Index out of range error when I know for sure that I am interating within the bounds of the array.Please see below code:let myArray: [String] = [] func getArray() -> [String] { // In real life, I don't know what the array is; but for convenience's sake, suppose it is [a,b,c] return ["a","b","c"] } for i in 0..<getArray().count { myArray[i] = getArray[i] }That is it. Basically, I am trying to initialize the elements of myArray by copying another array (in this case returned by getArray( ).As you can see, the above task is very simple, straightforward, and short. I don't understand where it's failing.If my understanding is correct (a big assumption since I am new to Swift), the getArray( ) has 3 elements, so getArray( ).count should return 3. And because the range specified is "<", it does NOT contain 3, so 0..<3 should be [0,1,2]...so everything should work fine?Clearly, I am missing something here? Anyone care to shed some light on this? I don't know why code as innocuous as this would cause any problems.FWIW, I am using Xcode 10.1 on Mac OS 10.14.5.Thanks in advance,KC
Posted
by kc_dev.
Last updated
.
Post not yet marked as solved
4 Replies
5.3k Views
Hi Apple Developer Community,Swift noob here.I was subclassing UICollectionViewCell when I realized I needed to initialize it in some way. So far, I have found init(frame: CGRect), init?(coder aDecoder:NSCoder), and awakeFromNib( ).Which is the preferred way to initialize a custom UICollectionViewCell (or a custom calss in general)?Thanks in advance! 🙂KC
Posted
by kc_dev.
Last updated
.
Post not yet marked as solved
4 Replies
623 Views
Hi Apple Developer Community,Is there a simple and *clean* way to get a substring of a given string?Suppose I want the first and last char removed from a string. In swift, I can dolet myString = "My String" print(myString[myString.index(myString.startIndex, offsetBy:1)..<myString.index(myString.endIndex, offsetBy:-1)]which is extremely long, complicated, and UGLY. In fact, it is so long, complicated, and ugly that it's probably better to use multiple lines (which feels rediculous for such a simple task).Surely, there's a better, cleaner way to do this?For example, in java, I can simple just doString myString = "My String" System.out.println(myString.substring(1,myString.length-1))which is much cleaner and easier to read. Is there something like this in Swift?Thanks in advance 🙂KC
Posted
by kc_dev.
Last updated
.
Post not yet marked as solved
2 Replies
902 Views
Hello Apple Developer Community,First of all, I am a total newbie to the Swift programming language.I am trying to ship my first app with Xcode. I thought it would make sense to first educate myself about the basics of Xcode and Swift. I've been reading this article as a starting point: https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/index.html#//apple_ref/doc/uid/TP40015214-CH2-SW1Coming from java, I find the syntax of Swift to be extremely complicated, obscure, unintuitive, and frankly hard to understand.Check the following statement for example, found in Working with Table Views >> Implement Navigationguard let button = sender as? UIBarButtonItem, button === saveButton else { os_log("The save button was not pressed, cancelling", log: OSLog.default, type: .debug) return }So I have a few questions about the above code:I understand that the as keyword is the down-cast operator. But why is there a "?" after it? What's the difference between as and as?I thought the else clause comes immediately after the initial statement, in this case button = sender. What's the equality check button === saveButton after the comma? Does this mean that the downcast has to work AND button must equal saveButton in order for the else clause at the end to not fire?Many thanks in advance! And thank you for your patience 😉KC
Posted
by kc_dev.
Last updated
.