Saturday, February 13, 2021

How to run an Xcode project on an iOS device without payment for the US$99 Apple Developer account (2021)

To run an Xcode project on an iPhone/iPad without paying US$99 for Apple Developer account, this error message may happen:

Your development team has reached maximum number of registered iPhone devices.

According to StackOverflow, I got this warning because I have a previous paid account but I cannot configure it at this stage. So I register a new and free Apple Developer account. and add it to Xcode with Preferences->Accounts.

The project with Xcode 11.6 is configured as below:


After selecting the new Apple Developer account as the team and making sure that the bundle identifier is new, the deployment becomes successful!

References:

Development team has reached maximum number of registered iPhone devices (StackOverflow)

How to Test iOS App without Developer Account?

Run an Xcode project on an iOS device without US$99 Apple Developer account (Configuration for iPhone) (StudySwift)

Saturday, February 6, 2021

Xcode 11.6 (Swift 5.2.4) - Hello World and how to set the background color programmatically

 code:

import SwiftUI


struct ContentView: View {

    let screenWidth = UIScreen.main.bounds.width

    var body: some View {

        ZStack {

            Color.yellow

            .edgesIgnoringSafeArea(.all)

            Text("Hello, World!")

            

            Text("Study Swift")

            .position(CGPoint(x: screenWidth/2, y: 400))

            

        }

    }

}


struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView()

    }

}

Result:


Reference:


How to set a background color for the viewController in swiftUI? (StackOverflow)

Wednesday, September 30, 2020

Thursday, August 27, 2020

Mac Technique: Ctrl + Alt + Del command for Windows on Mac

For using the Ctrl + Alt + Del command for Windows with a Mac keyboard, hold the following keys:


Control + option (alt) + fn + delete


References:

Ctrl + Alt + Del on a Mac with Windows (superuser)

Mac Technique: Delete Key for Windows on Mac

Thursday, June 7, 2018

Complex numbers - simple calculations

This example is tested with Xcode 9.2 playground in Swift 4.0.3. Basic complex number calculations such as the magnitude are includes as below:


class ComplexNum {
    var real : Float
    var imag : Float
    
    init (real: Float, imag: Float){
        self.real = real
        self.imag = imag
    }
    func show()->String {
        let plusChar = (imag>0) ? "+" : "-"
        return "\(real) \(plusChar) \(abs(imag))i"
    }
    //Magnitude
    func mag()->Float {
        return sqrt(self.real*self.real+self.imag*self.imag)
    }
    //Quick calculation with (i)
    func multiplyPlusI(num:ComplexNum) -> ComplexNum{
        return ComplexNum(real: -num.imag, imag: num.real)
    }
    //Quick calculation with (-i)
    func multiplyMinusI(num:ComplexNum) -> ComplexNum{
        return ComplexNum(real: num.imag, imag: -num.real)
    }
}
func addComplex(a: ComplexNum, b: ComplexNum)->ComplexNum {
    return ComplexNum(real: a.real+b.real, imag: a.imag+b.imag)
}
func multiplyComplex(a: ComplexNum, b: ComplexNum)->ComplexNum {
    return ComplexNum(real: a.real*b.real-a.imag*b.imag, imag: a.real*b.imag+a.imag*b.real)
}

let x = ComplexNum(real: 3, imag: 1)
print("x = \(x.show())")
print("|x| = \(x.mag())")
print("x(j) = \(x.multiplyPlusI(num: x).show())")
print("x(-j) = \(x.multiplyMinusI(num: x).show())")

let y = ComplexNum(real: -1, imag: -2)
print("y = \(y.show())")
print("|y| = \(y.mag())")
print("y(j) = \(x.multiplyPlusI(num: y).show())")
print("y(-j) = \(x.multiplyMinusI(num: y).show())")

print("x + y = \(addComplex(a: x, b: y).show())")
print("x * y = \(multiplyComplex(a: x, b: y).show())")

Results:


x = 3.0 + 1.0i
|x| = 3.16228
x(j) = -1.0 + 3.0i
x(-j) = 1.0 - 3.0i
y = -1.0 - 2.0i
|y| = 2.23607
y(j) = 2.0 - 1.0i
y(-j) = -2.0 + 1.0i
x + y = 2.0 - 1.0i
x * y = -1.0 - 7.0i

Related Information:
Matlab: Real and imaginary parts of complex number and multiplication with imaginary unit j

Friday, April 6, 2018

Mac Technique: How to Enable the Hidden Security & Privacy Option to Allow Apps from Unidentified Developers

With macOS High Sierra 10.13.4, there is no option by default to allow apps from unidentified developers in  -> System Preferences -> Security & Privacy -> General:


Close the window and type the following command in terminal:

sudo spctl --master-disable

and enter your password. Now go back to and the previously hidden Anywhere option is displayed:


Note that if you change a more secure option, the Anywhere option is hidden again when you turn on the Security & Privacy window next time.

Monday, April 2, 2018

Mac Technique: Dr. Eye Chinese-English Bilingual Dictionary with macOS 10.13.4 譯典通英漢雙向字典

Dr. Eye (譯典通), a famous Chinese-English bidirectional dictionary, is now available free with macOS High Sierra 10.13.4.

After installing the latest version of macOS, select Dictionary -> Preferences and 譯典通英漢雙向字典(Traditional Chinese-English).


Now both English and Chinese words may be looked up in the dictionary:



It is interesting that the Bopomofo (ㄅㄆㄇㄈ) Zhuyin Mandarin Phonetic Symbols (注音符號) used in Taiwan are displayed in this dictionary.

The Dr. Eye dictionary is also available with iOS 11.3.

Friday, March 9, 2018

C language with Mac: Hello World with Mac's Terminal

This post shows how to execute a hello world program in C with the following steps:

1. In terminal, create a C file called hello.c:

nano hello.c

2. Edit the hello.c file as:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

3. In terminal, execute the file with:

cc hello.c
./a.out

Result:


References

How to run C program on Mac OS X using Terminal? (StackOverflow)
How to print Hello World in C on Mac (YouTube)
C++ with Mac: Hello World with Mac's Terminal
Matlab: Run Hello World files in C/C++ with MEX command

Thursday, March 1, 2018

C++ with Mac: Install Boost Libraries

To install the Boost C++ libraries on a Mac

1. Download the latest Boost package, e.g. the boost_1_66_0.tar.bz2 file.

2. In terminal, move the the directory with the downloaded file and type:

tar --bzip2 -xf boost_1_66_0.tar.bz2

3. There are three types of Boost libraries:

(1) Header-only libraries. Just include these files without compilation. Most files belong to this type.
(2) Libraries required to be built. e.g. Boost.System, Boost.Thread, and Boost.Timer.
(3) Optional separately-compiled binaries, e.g. Boost Math, and Boost Random.

4. Follow the instructions in section 5.1 "Easy Build and Install" of Boost's getting started guide for unix-variants.

5. Follow the instructions in section 6 "Link Your Program to a Boost Library" of Boost's getting started guide for unix-variants. The main way A to link libraries may be like this command:

c++ -I ~your_path/boost_1_66_0/ test.cpp -o test ~your_path/boost_1_66_0/stage/lib/libboost_regex.a

Follow instructions in section 6.2. The result should be like this:



References

Boost libraries (boost.org)
Install boost on Mac OSX
Getting Started on Unix VariantsGetting Started on Unix Variants

Friday, February 23, 2018

C++ with Mac: Hello World with Mac's Terminal

This post shows how to execute a C++ hello world program with the following steps:

1. In terminal, create a C++ file called hello.cpp:

nano hello.cpp

2. Edit the hello.cpp file as:

#include <iostream>

using namespace std;

int main() {
    cout << "Hello, World!\n";
    return 0;
}

3. In terminal, execute the file with:

g++ hello.cpp
./a.out

Result:


References

Compiling simple Hello World program on OS X via command line (StackOverflow)
C language with Mac: Hello World with Mac's Terminal
Matlab: Run Hello World files in C/C++ with MEX command

Saturday, February 10, 2018

Saturday, September 23, 2017

UIButton Selector Error with Swift 4: Argument of '#selector' refers to instance method that is not exposed to Objective-C

Swift 4 (Xcode 9.0) does not work with the below Swift 3 code while drawing a UIButton:


The error message says that:

Argument of '#selector' refers to instance method 'click()' that is not exposed to Objective-C

Add '@objc' to expose this instance method to Objective-C

By clicking the "Fix" button, Xcode automatically fix this error by placing '@objc' in front of func click() as below:



The complete code to draw a button that prints a message while touching it:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
        button.center = view.center
        button.setTitle("Press", for: .normal)
        button.setTitleColor(.blue, for: .normal)
        button.setTitleColor(.cyan, for: .highlighted)
        button.addTarget(self, action: #selector(click), for: UIControlEvents.touchUpInside)
        view.addSubview(button)
    }
    @objc func click() {
        print("Pressed!")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}


Saturday, August 19, 2017

Mac Technique: Slide show for pdf file with Preview

To display the slide show for a pdf file in Preview, open the pdf file with Preview and press the hotkey:

[command ⌘] + [shift] + [F] 

or select:

View -> Slideshow





Saturday, August 5, 2017

Run an Xcode project on an iOS device without US$99 Apple Developer account (Configuration for iPhone)

Since iOS 9, it is possible to install the app of an Xcode project to an iOS device without paying for a US$99 Apple Developer account. The steps to download the app to an iPhone/iPad are demonstrated with an iOS 10 device as below:

When press the build and run button of Xcode:

You may see a Could not launch "deviceName" dialog like this:


So follow the instruction on the Xcode dialog to select General -> Profiles & Device Management on the iOS device (iPhone/iPad).


Select the device.
Select "Trust (Apple ID)".
 Select "Trust".

Then the app should run on the iPhone / iPad.

For more information about Xcode configuration, see this:

Friday, August 4, 2017

Visualized custom color in swift code with Color Literal

This post is written with Xcode 8.3.3 and Swift 3.1.

It is very common to set a UIColor with autocomplete as below:



However, there is a better way to select a custom color visually.
Type Co to find Color Literal with Xcode' automatic complete feature.


Then select a color. There are more color options with the color literal than with UIColor.colorName.


So we can see the color in the code instead of strings such as orange, red, ... ect.

There are also literal icons/images. For more information, see:

Be Literal! – iOS App Development

Monday, June 26, 2017

Mac Technique: Show the desktop with F11 key

To show the desktop with Mac, simply press [fn] + [F11] on a Mac's keyboard or press [F11] on an external USB keyboard.

Saturday, May 27, 2017

How to Sort Array, Dictionary, and Array of Tuples

The code below shows how to sort an array, a dictionary or an array of tuples in Swift 3.1 with Xcode 8.3.1 Playground. Sorting can be done in ascending or descending order. A dictionary can be sorted by key or by value.

Note: When sorting a dictionary, the returned type is an array of tuples.

//Array
let array = [3, 5, 9, 7, 4, 1, 2]

let arrayInc = array.sorted()
let arrayDec = array.sorted(by: >)

//Dictionary
let dict = ["A": 123, "B": 789, "C": 567, "D": 432]

print(dict)

let dictKeyInc = dict.sorted(by: <)
let dictKeyDec = dict.sorted(by: >)

print(dictKeyInc)
print(dictKeyDec)

let dictValInc = dict.sorted(by: { $0.value < $1.value })
let dictValDec = dict.sorted(by: { $0.value > $1.value })

print(dictValInc)
print(dictValDec)

for item in dictValDec {
    print("key:\(item.key) value:\(item.value)")
}

//Array of Tuples
let tupleArray = [("A", 123), ("B", 789), ("C", 567), ("D", 432)]

let tupleArrayInc = tupleArray.sorted(by: { $0.1 < $1.1 })


print(tupleArrayInc)


Result:

["B": 789, "A": 123, "C": 567, "D": 432]
[(key: "A", value: 123), (key: "B", value: 789), (key: "C", value: 567), (key: "D", value: 432)]
[(key: "D", value: 432), (key: "C", value: 567), (key: "B", value: 789), (key: "A", value: 123)]
[(key: "A", value: 123), (key: "D", value: 432), (key: "C", value: 567), (key: "B", value: 789)]
[(key: "B", value: 789), (key: "C", value: 567), (key: "D", value: 432), (key: "A", value: 123)]
key:B value:789
key:C value:567
key:D value:432
key:A value:123
[("A", 123), ("D", 432), ("C", 567), ("B", 789)]

Reference

Sort Dictionary by Key Value
cannot assign value of type '[(string, string)]' to type '[string : string]'

Monday, May 22, 2017

Dictionary of Arrays in Swift 3

The code below shows how to include an array in a dictionary in Swift 3.1 with Xcode 8.3.1 Playground.

dictionarySemitones["abc"]=[1,2,3]
dictionarySemitones["def"]=[4,5,6]

print(dictionarySemitones)
print(dictionarySemitones["abc"]!)

Reference:

A Dictionary of Arrays in Swift

Sunday, May 21, 2017

Convert an Integer Array to a String

The code below shows how to convert an integer array to a string in Swift 3.1 with Xcode 8.3.1 Playground.

let arrayInt = [1, 2, 3, 4, 5]
let string = "\(arrayInt)"
print(string)

Reference:

How can I convert an Int array into a String? array in Swift

Conversion from Numbered Musical Notation to Musical Note Letters

The code below converts Numbered Musical Notation (簡譜) to musical note letters using Xcode 8.3.2 playground in Swift 3.1. Techniques used in the code involves:

1. Separating a string into an array
2. Type casting each array element into an integer

3. Converting the numerical notation into a matched letter
4. Joining all letters into a single string

//Twinkle, Twinkle, Little Star 一閃一閃亮晶晶
let numberedNotation = "1 1 5 5 6 6 5"

//1. Separating a string into an array let numberedNoteArray = numberedNotation.components(separatedBy: " ")

let lookupTable = ["C","D","E","F","G","A","B"]

var noteArray = [String]()

for note in numberedNoteArray {
    //2. Type casting each array element into an integer
    let noteInt = Int(note)!

    //3. Converting the numerical notation into a matched letter
    let noteChar = lookupTable[noteInt-1]
    noteArray.append(noteChar)
}

//4. Joining all letters into a single string let noteString = noteArray.joined(separator: " ")

print("\(noteString)")

Result:

C C G G A A G