XCode: How to debug release mode build?

When you build your application in Release mode, XCode compiles the app with lots of optimizations, but debuggers hate optimizations, it will complain about this in the console output.

[Project] was compiled with optimization - stepping may behave oddly; variables may not be available

Actually some breakpoints might work, but the inspected object values most likely are not correct.

So to reliably debug the app, you need to switch it to Debug mode or turn off the optimization level like below in Build Settings:

xcode_optlevel

Xcode 9: Error from building Swift 2.x targets

If you try to open some older projects from Xcode 7.x which uses Swift 2.0, you might see following errors:

The target ‘xyz’ contains source code developed with Swift 2.x
Xcode 9 does not support building or migrating Swift 2.x targets.

Use Xcode 8.x to migrate the code to swift 3.

 

At this point, XCode 9.0 does not offer any assistance on Swift migration from 2.x to 4.x. It seems it abandon the support of this Swift version.

There is a switch in the ‘Build Setting’, offering the choice of Swift 3.2 and Swift 2.0 only.

swift

Setting the Swift Language Version to ‘Swift 3.2’ can obscure the error into compiler warnings.

The most straight-forward solution is to follow its advice: use XCode 8.x first to migrate the old Swift 2.x syntax to the latest Swift 3, and then open the project with XCode 9.

So …. don’t throw away the XCode 8 just yet.

 

 

 

 

 

XCode: Duplicate simulators appear in simulator list

 

You may experience this situation in XCode where the simulator list have some duplicated entries. See below screenshot:

 

replicated_sims.png

It might happens when you have more than one version of XCode installed or have XCode being updated.

Two approaches for fixing this:

  1. Manually delete the errant entries in XCode.
    Steps:
    – Go to XCode/Windows/Devices
    – Manually remove the duplicated ones.
  2. Reconstruct the simulator list from scratch. The steps are:
    1. Open Terminal
    2. sudo killall -9 com.apple.CoreSimulator.CoreSimulatorService
    3. rm -rf ~/Library/Developer/CoreSimulator/Devices
    4. reopen Xcode and re-add the simulators from XCode/Windows/Devices

 

 

 

 

How to clean up tons of disk space from XCode

Here are some tips on getting rid of junk files related to XCode.

updated: Feb 15, 2022

Clear SwiftUI Preview

# Try to delete the preview canvas from here
~/Library/Developer/Xcode/UserData/Previews/Simulator Devices/

# then in command line run the following
killall -9 com.apple.CoreSimulator.CoreSimulatorService

# finally restart XCode

~/Library/Developer/Xcode/DerivedData
The Derived Data folder here contains all of your project’s intermediate build information, index and debug/release build targets. It’s OK to
delete the directories under this as XCode will always re-generate the files again when needed.

~/Library/Logs/CoreSimulator
All the system log and NSLog output to the console are also logged here, as a result, the files can grow very large gradually. It is safe to delete the whole directory. Xcode will recreate fresh instances in the next run of simulator.

~/Library/Developer/Xcode/iOS DeviceSupport
This is a place to symbolicate crash logs. Xcode will automatically create a folder with the device version.

~/Library/Caches/com.apple.dt.XCode
 Xcode will recreate its caches when needed (it can take some time at first relaunch, though, if Xcode needs to re-download something)

Archives
Over the time you might have accumulated a significant amount of archives. Either created for beta testers or actual App Store submissions.

Delete the one’s you don’t need in the Organizer, or go to ~/Library/Developer/Xcode/Archives/ to delete them in bulk.

LLVM module cache

These two directories are caches created by LLVM compiler. Execute these two commands:

rm -rf “$(getconf DARWIN_USER_CACHE_DIR)/org.llvm.clang/ModuleCache”

rm -rf “$(getconf DARWIN_USER_CACHE_DIR)/org.llvm.clang.$(whoami)/ModuleCache”

Format String Issue Using NSInteger

The compiler that comes with latest Xcode now have additional warnings, some of them can help you deal with the 64-bit migration.

For example, in this snippet of code:

NSInteger iVal = 1;
NSLog(@“%ld”, iVal);

You will see this warning:

warning: values of type ‘NSInteger’ should not be used as format arguments; add an explicit cast to ‘long’ instead

Background
OS X/iOS uses several data types such as NSInteger, NSUInteger,CGFloat to provide a consistent means of representing values in 32- and 64-bit environments.

In a 32-bit environment, NSInteger and NSUInteger are defined as int and unsigned int, respectively.

In 64-bit environments, NSInteger and NSUInteger are defined as long and unsigned long, respectively.

Something like that in the Foundation header files:

#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif

To avoid the need to use different printf-style type specifiers depending on the platform, you can use the specifiers shown here:

Type Format specifier Considerations
NSInteger %ld or %lx Cast the value to long
NSInteger %ld or %lx Cast the value to unsigned long
CGFloat %f or %g Both works for floats and doubles

Why this warning only happens in release build?
It is because in the auto-created build configuration, the flag of Build Active Architecture Only is defaulted to YES.
The target (simulator or plugged-in device) detected from Clang compiler is 64-bit.
The casting warning will not be raised as the NSInteger is mapped to ‘long’ already.

Alternative
Another way to avoid this warning is to box the NSInteger/NSUInteger variable in NSNumber, using the literal syntax.

    NSInteger iVal = 1;

    NSLog(@”%@”, @(iVal));

However, it may drag down the performance a bit if the log statement is called a lot.

 

 

About ARM architectures for iOS development

Introduction of ARM
All iOS devices released so far are powered by processors based on ARM architecture.

Note that architecture is different than physical processor. Architecture is a set of rules that govern the design of the processors, such as the approach (RISC or MISC), the instruction set etc.

Processor makers or computer makers license the ARM architecture when they manufactures the actual chips.
For example, Apple licenses the designs from ARM and integrate them into their own SoC (System on Chip).
The chip is then often manufactured by third-party chip maker like TSMC.

ARM is the most dominant architecture form used in mobile devices, compared to Atom or x86 architecture from Intel.

Processors based on ARM architecture is not just useful for mobile devices, it is also used on embedded system, game consoles or even desktop computers.

Specify ARM architecture on Xcode
The ARM architecture comes in a few different versions developed over time; each one added some new instructions and other improvements, while being backwards compatible with the previous versions. The first iPhone had a processor that implements ARMv6 (short for ARM version 6), while the later devices have processors that can support ARMv7. The architectures later than ARM v7 are collectively called ARM64 in Xcode.

See the following table for the summary. For more details, consult http://iossupportmatrix.com/

CPU Architecture Used by
A5 ARM v6 (32-bit) iPhone 2G, 3G
A6 ARM v7 (32-bit) iPhone 3GS, 4, 4S
A6 Soc ARM v7s (32-bit) iPhone 5
A7 ARM64 iPhone 5s, iPad Air
A8 ARM64 iPhone 6
A9 ARM64 iPhone 6s
A10 ARM64 iPhone 7

So when you compile code, you specify the architecture version you’re targeting, and the compiler will restrict the instructions it generates to those available in that architecture version.

Here is the typical ‘Build Setting’ of the target from Xcode 8.0

ios_arch.jpg

 

Having more than one value means the compilation will generate ‘fat’ universal binary for all specified CPU architectures, including both 32-bit or 64-bit codes.

“Build Active Architecture Only” Setting
This setting allows you to save compilation time and reduce file size of the output binary as the compiler will generate the exact architecture for the plugged-in device. It is usually set to ‘Yes’ for Debug, and ‘No’ for Release.

 

 

 

 

 

 

 

 

 

 

 

 

 

How to install XCode 8.x with XIP format

Starting with Xcode 8 beta, all available download binaries are packaged in a format called .xip.
It is a measure that Apple has taken to prevent the following event from happening again.
https://9to5mac.com/2015/09/20/xcode-ghost-app-store-malware-malicious-apps/

What is .xip format?
A XIP file is analogous to zip file, but allows for a digital signature to be embedded and be verified on the receiving system, before the archive is expanded.

Verifying that a Package is Signed
When Apple’s Gatekeeper feature is set to only allow applications downloaded from the Mac App Store and identified developers, users can only install signed packages.

To verify that a package is signed, execute the following command on a computer with the package:

pkgutil –check-signature /path/to/package.pkg
If the output is “Status: signed by a certificate trusted by Mac OS X”, the package is signed. If the output is “Status: no signature”, the package is not signed.

However, the easiest way to expand the .xip file is to use Archive Utility.

 

Right-Click and open with Archive Utility from Finder.

Screen Shot 2016-10-28 at 4.02.03 PM.png

 

Screen Shot 2016-10-28 at 3.54.45 PM.png

After a while, the XCode.app will be extracted, copy the app into Application as usual.

When starting the Xcode app the first time, you will see the App being verified by macOS Gatekeeper process.

Basically since the app is downloaded it from a internet, Gatekeeper examines the entire bundle, making sure that there is no suspicious code. This may take a while, likely 10-20 minutes.

Screen Shot 2016-10-28 at 3.59.46 PM.png

If you don’t want to wait forever, this command might help by removing the

sudo xattr -d com.apple.quarantine Xcode.app

 

 

How to Fix Windows 10 Cumulative Update KB3189866 Getting Stuck at 45 or 95 Percent

Microsoft just released a cumulative update KB3189866 for Windows 10 Version 1607 on Sep 14, 2016.

Numerous users including me saw the issue that the update stuck at 45% or 95%.

Listen Microsoft, you better take down that Windows Update and fix this issue now.
Although people found a way to get around this, for causal users, we are tired of this kind of problem, it is simply a waste of time for everyone!!
I know Windows Update is not easy as it has to take care of years of legacy, but it seems the problem happens to be exponential algorithm employed in the evaluation of superseded updates. This complex dependency resolving very often tends to make your computer get stuck. So my advice to Microsoft: Hire some more computer scientists to fix this!! You can do better!!

Back from ranting, two approaches for the workaround:
1) Use Microsoft Catalog to download the update directly via this link:
http://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB3189866

Clicking the link on Edge and you will see this message:

“You’ve stumbled upon some vintage web tech
This website runs on older technology and will only work in Internet Explorer. “

The Microsoft Catalog is perhaps an ActiveX component and can only be executed from the old Internet Explorer 11 browser.

I really don’t like installing extra component on my machine, so I skipped this approach.

2) Second approach is better. Simply download the installer from these links, and launch the downloaded installer manually.

KB3189866_X64 (431MB)
http://download.windowsupdate.com/d/msdownload/update/software/secu/2016/09/windows10.0-kb3189866-x64_2c3e91a8162269579c8bf26a502c620d41efef0d.msu

KB3189866_X64 (255MB)
http://download.windowsupdate.com/d/msdownload/update/software/secu/2016/09/windows10.0-kb3189866-x86_48cc54806747877357a732fee03118eb38e92947.msu

Android Studio Tips – How to add image to ‘drawable’ folder

You can use the plain old trick of copy-and-paste file operation.

Steps:

  • Use CTRL-C/Command-C (Mac OS) to copy the image file
  • Go to ‘Project View’.
  • Highlight the ‘drawable’, press CTRL-V/Command-V to paste the file.
  • Android Studio will show this dialog, type in the folder name if you need to store it in different resolution folder.

Or you can use mouse drag-and-drop, be sure to hold CTRL key. Otherwise it will move the file instead.

image_add

How to create an Empty Application for iOS in XCode 7 without Storyboard

The cleanest and simplest way is to create a project template for XCode 7.

Copy the downloaded ‘Empty Application.xctemplate’ folder to

{Xcode.app}/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application/

Restart XCode and you can see a new templated in iOS called ‘Empty Application’.

Follow this link for more details.

https://github.com/featherJ/Empty-Application-For-Xcode-7