Showing posts with label swift-error. Show all posts
Showing posts with label swift-error. Show all posts

Friday, March 8, 2019

Swift Error: nib must contain exactly one top level object which must be a UITableViewCell instance

I'm developing a pet project, which requires loading a UITableViewCell from a XIB file. At first, it worked fine but after making some changes, I got the error when the system trying to load the UITableViewCell from a nib file to construct the UITableView object.

Cause


It's because I accidentally dragged a Tap Gesture Recognizer from the Object Library and dropped it on the UITableViewCell in IB.


Solution


Click on the Tap Gesture Recognizer icon and press Delete key.

Thursday, October 5, 2017

Terminating app due to uncaught exception 'CALayerInvalid', reason: 'layer is a part of cycle in its layer tree'

I'm using Xcode 8 and Swift 3 to develop an application for learning purpose. I have one controller and a table view. Everything was working fine at first. But after some silly modification in Interface Builder by a newbie like me, the app crashed when I started it in the simulator. Below is the full log message.

section=0, numOfRows=10
Item: name=Shiny Mac, serialNumber=Optional("2A725A93"), valueInDollars=70, dateCreated=2017-10-05 09:37:41 +0000
row=0, section=0
2017-10-05 16:37:41.594 Homepwner[62092:2091530] *** Terminating app due to uncaught exception 'CALayerInvalid', reason: 'layer <CALayer: 0x600000036240> is a part of cycle in its layer tree'
*** First throw call stack:
(
0   CoreFoundation                      0x0000000102251b0b __exceptionPreprocess + 171
1   libobjc.A.dylib                     0x00000001018a1141 objc_exception_throw + 48
2   CoreFoundation                      0x00000001022ba625 +[NSException raise:format:] + 197
3   QuartzCore                          0x0000000108867ea2 _ZN2CA5Layer30ensure_transaction_recursivelyEPNS_11TransactionE + 102
4   QuartzCore                          0x0000000108867eed _ZN2CA5Layer30ensure_transaction_recursivelyEPNS_11TransactionE + 177
5   QuartzCore                          0x0000000108867eed _ZN2CA5Layer30ensure_transaction_recursivelyEPNS_11TransactionE + 177
6   QuartzCore                          0x0000000108867eed _ZN2CA5Layer30ensure_transaction_recursivelyEPNS_11TransactionE + 177
7   QuartzCore                          0x000000010886fb3a _ZN2CA5Layer15insert_sublayerEPNS_11TransactionEP7CALayerm + 394
8   QuartzCore                          0x000000010886ff50 -[CALayer addSublayer:] + 180
9   UIKit                               0x0000000102e1d7ad -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1322
10  UIKit                               0x000000010311d9a0 -[UITableViewCell _addSubview:positioned:relativeTo:] + 357
11  UIKit                               0x0000000102e0bcf8 -[UIView(Hierarchy) addSubview:] + 838
12  UIKit                               0x000000010323b340 -[UITableViewCellLayoutManager layoutSubviewsOfCell:] + 2386
13  UIKit                               0x000000010326db12 -[UITableViewCellLayoutManagerValue1 layoutSubviewsOfCell:] + 71
14  UIKit                               0x00000001031271d7 -[UITableViewCell layoutSubviews] + 119
15  UIKit                               0x0000000102e2155b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1268
16  QuartzCore                          0x0000000108876904 -[CALayer layoutSublayers] + 146
17  QuartzCore                          0x000000010886a526 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 370
18  UIKit                               0x0000000102e0f334 -[UIView(Hierarchy) layoutBelowIfNeeded] + 1108
19  UIKit                               0x0000000102e1608e +[UIView(Animation) performWithoutAnimation:] + 90
20  UIKit                               0x0000000102ecac29 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 1125
21  UIKit                               0x0000000102ecacf8 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
22  UIKit                               0x0000000102e9f639 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2845
23  UIKit                               0x0000000102ed3ccc -[UITableView _performWithCachedTraitCollection:] + 111
24  UIKit                               0x0000000102ebae7a -[UITableView layoutSubviews] + 233
25  UIKit                               0x0000000102e2155b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1268
26  QuartzCore                          0x0000000108876904 -[CALayer layoutSublayers] + 146
27  QuartzCore                          0x000000010886a526 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 370
28  QuartzCore                          0x000000010886a3a0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
29  QuartzCore                          0x00000001087f9e92 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
30  QuartzCore                          0x0000000108826130 _ZN2CA11Transaction6commitEv + 468
31  QuartzCore                          0x0000000108826b37 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 115
32  CoreFoundation                      0x00000001021f7717 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
33  CoreFoundation                      0x00000001021f7687 __CFRunLoopDoObservers + 391
34  CoreFoundation                      0x00000001021dc038 CFRunLoopRunSpecific + 440
35  UIKit                               0x0000000102d5808f -[UIApplication _run] + 468
36  UIKit                               0x0000000102d5e134 UIApplicationMain + 159
37  Homepwner                           0x00000001012bf8a7 main + 55
38  libdyld.dylib                       0x00000001060b065d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

According to http://blog.benjamin-encz.de/post/ios-9-detects-cycles-in-layout-trees/, I found the problem. It's because I assigned the tableView object to the accessoryView property of the UITableViewCell. I knew that because when I selected the UITableViewCell in Interface Builder and opened the Connection Inspector, I saw the accessoryView is connecting to Table View in Outlets section. Please see the image below.



To understand about Outlets and Referencing Outlets sections, you can go to this post. According to this post, the accessoryView property (an instance of UIView) represents the small area to the right of the content view of a table view's cell. See the image below.


The table view has a cell. The cell has an accessory view. But, I made the table view as the accessory view of its own cell. This does not make sense and make a circular relationship between the table view and its cell.

The solution is deleting the connection between the accessoryView and Table View from the Outlets section.







UITableView does not show all items

I'm using Xcode 8 and Swift 3 to develop an application for learning purpose. The app used UITableView to display a list of items. I had 20 items but only 12 of them were shown in the simulator. I tried to scroll down to the bottom but no more items. Note that I'm using Macbook Air 11'' mid 2013. I tried to scroll in the simulator using 2 fingers as I always did on browsers and other applications.

After that, I found that it was not the problem with my codes. Everything was fine. It's just the behavior of UITableView. It loads only some items to display which can fit the device's screen. It would show more if you use 3-finger scroll. 3-finger scroll in the simulator works the same as 1-finger tap on mobile device.

Sunday, September 24, 2017

The UIButton is not responsive

I'm new to swift. My view controller has only one button. I added a target for it to handle the TouchUpInside event. It worked at first, but it became unresponsive after I made some changes to the UI. It was just like a UILabel. No matter how many times i clicked on it, nothing happened.

Then, i figured out that it was just my silly mistake. I changed its height to zero by accident, but the button still appeared in both Interface Builder and simulator. This is why I got confused. Unlike UIButton, UILablel does not appear in the simulator if its height is zero.

[LayoutConstraints] Unable to simultaneously satisfy constraints

I'm learning Swift 3 by using Xcode 8 on MacOS Sierra. My view controller has a simple button, and I created two constraints for the top space and bottom space of the button. When I launch the app in Xcode, I got the following log message.

2017-09-24 16:40:23.780953+0700 Learn Swift[31177:1017369] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
    "<_UILayoutSupportConstraint:0x600000090770 _UILayoutGuide:0x7ff359716960.height == 0   (active)>",
    "<_UILayoutSupportConstraint:0x60000008eb00 V:|-(0)-[_UILayoutGuide:0x7ff359716960]   (active, names: '|':UIView:0x7ff359715f40 )>",
    "<_UILayoutSupportConstraint:0x6000000906d0 _UILayoutGuide:0x7ff359716b20.height == 0   (active)>",
    "<_UILayoutSupportConstraint:0x600000090720 _UILayoutGuide:0x7ff359716b20.bottom == UIView:0x7ff359715f40.bottom   (active)>",
    "<NSLayoutConstraint:0x600000090950 V:[_UILayoutGuide:0x7ff359716960]-(113)-[NextButton]   (active, names: NextButton:0x7ff359716370 )>",
    "<NSLayoutConstraint:0x6000000909a0 V:[NextButton]-(465)-[_UILayoutGuide:0x7ff359716b20]   (active, names: NextButton:0x7ff359716370 )>",
    "<NSLayoutConstraint:0x60800008d430 'UIView-Encapsulated-Layout-Height' UIView:0x7ff359715f40.height == 568   (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000000909a0 V:[NextButton]-(465)-[_UILayoutGuide:0x7ff359716b20]   (active, names: NextButton:0x7ff359716370 )>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
There is a conflict between the constraints i created and the constraint the system created by default for the button. The height of the view controller's view is only 568 points, which is less than the sum of the button's height (29 points), top space (113 points), and bottom space (465 points). 

According to this page, we should lower the priority of the bottom space constraint (465 points) to 999 so the system will provide the selected bottom space, which is as close as it can while still satisfying the other constraints. 




Saturday, September 16, 2017

xcode-select: error: tool 'genstrings' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance

I'm using Xcode 8 on MacOS Sierra to develop an iOS application just for learning purpose. I run the command below to create a strings table file for a view controller class but got the error.


genstrings MapViewController.swift

I can't find Xcode in Applications directory. I don't know why as i'm new to it, and I don't want to find out now. However, I accidentally fixed it myself.

1. Open Xcode and go to Xcode preferences by pressing command + ,
2. Select Locations tab and select "Xcode 8.3.3 (8E3004b)" from the "Command Line Tools" combo box.

Tuesday, September 12, 2017

My iOS app does not load the UI completely

I'm developing an iOS application using Swift 3 and Xcode 8 on MacOS Sierra. I have one UITabBarController referencing 4 view controller. Here how it looks like in Interface Builder:


The first view controller below is supposed to be loaded as home screen.


But when run the project, it looked like in the image below:


And then, I looked at the Console and saw this message:

2017-09-12 16:07:40.740 Learn Swift[15358:519952] Could not load the "ConvertIcon" image referenced from a nib in the bundle with identifier "com.vathanakmao.iosapp.Learn-Swift"


Solution

It's because I don't have the "ConvertIcon" image so I remove unset the icon for the bar item of that view controller and the view controller's view loaded completely. To unset the icon, open Interface Builder and click on the bar item at the bottom of the controller. Then, open Attributes Inspector and remove the image from the Image textbox under Bar Item section.






Monday, August 21, 2017

Main.storyboard: Scene is unreachable due to lack of entry points and does not have an identifier for runtime access via -instantiateViewControllerWithIdentifier:.

I'm using Xcode 6.1 on Mac OS X Mavericks v10.9.5. The warning appeared when I have more than one view controllers in my story board.

To remove the warning, I set the Storyboard ID (in the Identity Inspector) for all the view controllers, which are not root view controller. The root view controller is the one with the Is Initial View Controller property checked (in Attributes Inspector).

The storyboard ID identifies the view controller within a storyboard. Storyboard ID is used when you want to programmatically access a view controller for some reasons such as switching to another view.

Got black screen when starting simulator

I'm using Xcode v6.1 on Mac OS X Mavericks v10.9.5. I'm new to iOS so i'm developing a simple app for learning. It's using two frameworks such as UI Kit and Foundation. When running my app in simulator, sometimes it worked but some other times i just showed black screen as shown in the image below.


As you can see on the title bar of the simulator, the app was built and run on iPhone 4s. Then I changed the destination device to iPhone 5 or newer, it worked. I don't know why but perhaps my application is using some classes or libraries which are not supported by iPhone 4s.

To change the running device, click on Product menu then Destination and select iPhone 5. After that, clean the project (command+shift+K), and build and run (command+R) it again.