(2022-23, 2019, 2018 (没有标记就是都有,标记就是当年的题) )
Suppose that you want to write a model class Converter for money conversions, which has
the following elements:
1. The properties of this class are given by:
a. the names of the two currencies in the conversion as stored properties.
b. the rate used in the conversion as stored property with the observers willSet and didSet.
c. the inverse rate as calculated property with a getter and setter.
2. The methods of the class are for:
a. initialize the properties.
b. convert an amount based on a direction
func convert(amount:Double, direction:Int) ->Double
where direction is 1 for first-to-second conversion and 1- for the opposite.
You are required to write the code for the model class [Link] to cover the following
points:
(2019, 22) i) The declaration of the stored property called "rate" with the willSet and didSet
observers. (5 marks)
(2019, 22) ii) The declaration of the calculated property "inverseRate" with the get and set
blocks. (5 marks)
(2019) iii) The initializer init(curNamel:String, curName2:String, rate:Double) to initialize the
stored properties. (5 marks)
(2019, 22) iii) The function converts to make the conversion of amount based on given
direction and to return the converted amount. (5 marks)
(2018) You are required to write the code for the model class [Link]. (20 marks)
2022-2018 Q1 full code answer
(2021-2022)
Suppose that you want to write a model class for linear conversions, which can cover any type
of conversions (money, lengths, weights, temperature etc) using a linear transformation e.g.
scaling by a rate. The class has the following elements:
1. The properties of this class are given by:
a. the names of the input and output elements involved in the conversion in the conversion
as stored properties.
b. the rate involved in the direct conversion as stored property
c. the inverse rate used in the inverse conversion as calculated property.
2. The methods of the class are for:
a. initialize the properties.
b. convert an amount based on a direction
func convert(amount:Double, direction:Int) ->Double
where direction is 1 for the direct conversion and -1 for the opposite.
i) You are required to write the code for the model class [Link]. (15 marks)
(2021, 22) Explain 2 positive differences between coding a class in swift and coding a class
in some other language you know e.g. python. (5 marks)
1. Type Safety and Compile-Time Checks
● Swift is strongly typed, meaning the compiler checks types before the program
runs, reducing bugs and ensuring code robustness.
● Python is dynamically typed, which allows for faster coding but increases the risk
of runtime errors due to its more flexible type system.
2. Memory Management and Performance
● Swift uses ARC for efficient memory management by automatically deallocating
unused objects, enhancing performance, particularly on resource-limited mobile
devices.
● Python relies on garbage collection, which can intermittently pause the program
to clean up memory, potentially leading to less efficient performance.
(2022-23 Q2)
Suppose that you have to build an iPhone interface for money conversion using multiple
currencies. The currencies are placed in 2 spinners (UIPickerView), where a currency is chosen
for the conversion to and from it. The storyboard has a ViewController with the following UI
components: two spinners to select the currencies, two text fields and two buttons with the
labels Clear and Convert. You must use a model Converter object in this iPhone app and give
functionality to convert in both ways. The currency names and rates are given in two arrays
currencyNames and currencyRates. Write the code for [Link] to include:
i) Explain how to populate the UIPickerView with the data from currencyNames. (5 marks)
ii) Give the code of viewDidLoad() showing how the outlets are used. (5 marks)
iii) Give the code of the IBAction method for the button “Convert”. (10 marks)
** (iv) 题的答案在后面
(2021 Q2)
Suppose that you have to build an iPhone interface for Kilogram to Stone conversion. The
storyboard has a ViewController with the following UI components: two labels for the
conversion names, two text fields and two buttons with the labels Clear and Convert. You must
use a model Convertor object in this iPhone app and give functionality to convert in both ways.
Write the code for [Link] to include:
i) The outlets and how they are used in viewDidLoad(). (5 marks)
ii) The action functions for the 2 buttons. (5 marks)
(2019/2018 Q2 整体的代码结构其实和21年的一样,只是变成doller 和 pound,)
Suppose that you have to build an iPhone interface for Dollar to Pound conversion. The
storyboard has a ViewController with the following UI components: two labels for the currency
names, two text fields and two buttons with the labels Clear and Convert. You must use a model
Convertor object in this iPhone app and give functionality to convert in both ways. Write the
code for [Link] to include:
(2019) i) The outlets and how they are used in viewDidLoad(). (5 marks)
(2018+2019) ii) The action functions for the 2 buttons. (5 marks)
(All years) iii) Describe the MVC model and its communication for this application.
* 根据application稍微作出描述上的调整,整体大致不变,下面是money currency app为例子
Model: The model handles the core logic of the application, such as storing and
managing currency data
View: A view is the part of the interface that the user directly sees and operates on,
such as buttons and wheel selectors
Controller: The controller is ConverterViewController, which is between the model and
the view and is used to handle user input and interface updates.
MVC Communication Pattern
User interaction with the view: The user manipulates interface elements such as buttons and
selectors to make currency selections and amount inputs.
View notification controller: When the user performs an operation such as clicking the
"Convert" button, the view notifies the controller through IBAction.
Controller processes data: The controller receives user input from the view and passes these
inputs to the model for processing.
The model processes and returns data: The model performs calculations based on the data it
receives and returns the results to the controller.
The controller updates the view: After the controller gets the result returned by the model, it
updates the view to display the final currency conversion result.
(2022-23 Q3)
Suppose that you have to build a database driven iPhone app to expose some data in a table.
i) Give two reasons and some explanation why core data is a better or a worse approach
than SQLite. (5 marks)
Core Data vs SQLite
1. Ease of Use and Abstraction: 易用性和抽象化:
Core Data simplifies database interactions through high-level objects and relationships,
allowing developers to focus more on application logic and less on SQL knowledge.
2. Integration with iOS: 与iOS的集成:
Core Data is tightly integrated with iOS, supporting automatic synchronization between data
and UI, which facilitates the development of responsive applications and enhances user
experience.
ii) Explain the steps required to make this core data application. (5 marks)
1. Add Core Data to Project: Enable Core Data when starting a new Xcode project, or add a
.xcdatamodeld file to an existing project.
2. Create Data Model: Define entities and their attributes in the .xcdatamodeld file.
3. Set up Core Data Stack: Configure NSPersistentContainer in the AppDelegate to manage
the database.
4. Manage Data: Use NSManagedObjectContext for handling data operations like adding,
deleting, and editing records.
5. Save Changes: Use the save() method on the context to store changes permanently.
iii) Explain how the table is then populated with the data from the core data. (5 marks)
1. Setup NSFetchedResultsController: Set it up with a fetch request for the desired
entity and sort order to manage and fetch data.
2. Setup UITableViewDataSource: Use data from the fetchedResultsController to
populate the table cells.
3. Handle Updates with NSFetchedResultsControllerDelegate: Responds to data
changes and allows the representation to be dynamically updated, such as
inserting or deleting rows.
4. Load and Refresh Data: Run performFetch() to load data and [Link]() for
updates.
iv) Give the code that makes the frc (fetched results controller) object. (10 marks)