Shree. IOS Development CT - 2 Solutions
Shree. IOS Development CT - 2 Solutions
Shree. IOS Development CT - 2 Solutions
Ios Development
12 Marks :
Enumeration:
enum CompassPoint {
case north
case south
case east
case west
}
```
Enums can also have associated values or raw values. Associated values allow
you to attach additional data to each enum case, while raw values associate a
pre-defined value of a certain type with each case.
enum Barcode {
case upc(Int, Int, Int, Int)
case qrCode(String)
}
switch direction {
case .north:
print("Heading North")
case .south:
print("Heading South")
case .east:
print("Heading East")
case .west:
print("Heading West")
}
Switch statements in Swift are exhaustive, meaning that every possible value of
the expression being switched on must be handled by one of the switch cases. If
you're working with an enum, Swift will often require you to handle all of its
cases explicitly, which helps catch potential issues at compile-time.
Switch statements can also include compound cases, where multiple cases share
the same code execution:
let approximateCount = 62
let countedThings = "moons orbiting Saturn"
var naturalCount: String
switch approximateCount {
case 0:
naturalCount = "no"
case 1..<5:
naturalCount = "a few"
case 5..<12:
naturalCount = "several"
case 12..<100:
naturalCount = "dozens of"
case 100..<1000:
naturalCount = "hundreds of"
default:
naturalCount = "many"
}
We'll start by defining a protocol that specifies the behavior a delegate must
implement. In this case, let's call it TemperatureConverterDelegate.
Next, we'll create a class responsible for converting temperatures. This class will have
a delegate property, conforming to the TemperatureConverterDelegate protocol.
class TemperatureConverter {
weak var delegate: TemperatureConverterDelegate?
Now, we'll implement a class that acts as the delegate and conforms to the
TemperatureConverterDelegate protocol. This class will handle the conversion results.
In the main part of your code, you create instances of the converter and the delegate
handler, and set up the delegation relationship.
converter.delegate = resultHandler
// Perform conversions
converter.convertToFahrenheit(fromCelsius: 20.0)
converter.convertToCelsius(fromFahrenheit: 68.0)
Explanation:
• Protocol Definition: We define a protocol TemperatureConverterDelegate that
requires a method didConvertTemperature(_:) to be implemented by
conforming classes. This method will be called when a temperature conversion
is completed.
• Converter Class: The TemperatureConverter class has methods for converting
temperatures between Fahrenheit and Celsius. When a conversion is done, it
calls the delegate's didConvertTemperature(_:) method with the result.
• Delegate Implementation: The ConversionResultHandler class implements the
TemperatureConverterDelegate protocol. It provides the didConvertTemperature(_:)
method to handle the converted temperature. In this example, it simply prints
the result, but in a real application, you might use this result in various ways.
• Setting up Delegation: In the main part of the code, we create instances of
the converter and the delegate handler. We then set the delegate property of
the converter to the instance of the delegate handler. This establishes the
delegation relationship.
• Performing Conversions: Finally, we call the conversion methods of the
converter. The delegate's didConvertTemperature(_:) method will be called
automatically upon completion of each conversion.
4 Marks :
1. Purpose of using number Formatters in dismissing keyboard
Ans :
Number formatters play a crucial role in dismissing the keyboard in Swift when
dealing with text fields that expect numerical input. The purpose of using
number formatters in this context is to ensure that the input provided by the user
adheres to a specific numeric format, such as currency, decimal precision, or
thousand separators.
1. Input Validation: Number formatters allow you to validate the user's input
as they type. By specifying a certain format or range, you can ensure that the
user enters valid numerical data. For instance, if you're expecting currency
input, you can set the number formatter to the currency style and validate that
the input conforms to the currency format.
2. Formatting While Typing: Number formatters can format the input text as
the user types, providing a formatted representation of the numerical value. For
example, if the user is entering a large number, the formatter can automatically
add thousand separators for better readability. This formatting happens
dynamically, reflecting the changes in the text field in real-time.
2. Internationalization
Ans :
3. Various Steps involved to create simple ios with example
Sol :
Creating a simple iOS app involves several steps, from setting up your development
environment to writing code and testing your app. Here are the basic steps involved:
Animation Completion:
Usage: When you want to perform some action immediately after an animation
completes, such as updating UI elements, changing view properties, or
triggering another animation, you can use the completion block of the animation
API to specify the code that should run once the animation finishes.
Example (Swift):
Animation Constraints:
Usage: When you want to animate changes to a view's layout, such as moving a
view to a new position, resizing it, or changing its alignment, you can animate
the changes to the view's constraints. This is often done by modifying the
constant values of layout constraints within animation blocks.
Example (Swift):
Key Differences:
1. Purpose: Animation completion is used to execute code after an animation
finishes, while animation constraints are used to animate changes to layout
constraints.
3. Scope: Animation completion can be used with any type of animation (e.g.,
frame animations, transform animations), while animation constraints
specifically involve animating layout constraint changes.
Instance Methods:
Instance methods are functions that are associated with a particular type or class and
are called on instances of that type or class. These methods can operate on the
instance's properties and perform tasks relevant to the type they belong to.
Syntax:
In this example:
Instance methods can access and modify instance properties and other instance
methods within the same class. They are fundamental in object-oriented
programming as they encapsulate behavior specific to instances of a class.
• SomeClass defines a subscript that takes an integer index and returns a string.
• We create an instance of SomeClass called myInstance.
• We access the subscript of myInstance using square brackets [0].
Loops:
Loops are control structures in Swift that allow you to execute a set of statements
repeatedly until a certain condition is met. There are mainly three types of loops in
Swift:
1. for-in Loop: Iterates over a sequence, such as ranges, arrays, dictionaries, or
other collections.
repeat-while Loop: Similar to a while loop, but the condition is evaluated after the code
block has executed, ensuring that the code block is executed at least once.
String Interpolation:
Enumeration Methods:
Enumeration methods are methods provided by Swift's standard library that allow
you to iterate over collections, perform operations on their elements, and transform
them in various ways. Some common enumeration methods include map , filter,
reduce, forEach, etc.
In this example:
In short, when you add subviews to a stack view, the stack view automatically creates
constraints to manage the spacing, alignment, and sizing of those subviews within
the stack view. These constraints are created implicitly by the stack view, without the
need for explicit constraint definitions in your code.
The stack view calculates these constraints based on its configuration properties such
as axis, alignment , distribution, spacing, and the intrinsic content sizes of its subviews.
These implicit constraints make it easier to create complex layouts with dynamic
content, as the stack view handles much of the layout management automatically.
However, it's essential to understand how these implicit constraints work to ensure
that your UI behaves as expected, especially when dealing with more complex
layouts or when needing to customize the stack view's behavior further.
Named Types:
Named types are types that you define with a name, typically using a struct, class,
enum, or protocol declaration. They represent a single value or a collection of related
values. Examples of named types include structs, classes, enums, and protocols.
1. Structs: Structs are value types that encapsulate data and behavior related to
a particular entity. They are often used to model simple data structures.
Enums: Enums define a group of related values, often representing a finite set of options or
states.
Compound Types:
Compound types are types that are composed of multiple values. They include tuples
and arrays.
1. Tuples: Tuples are a lightweight way to group multiple values into a single
compound value. They can contain values of different types and are useful for
temporary groupings.
Arrays: Arrays are ordered collections of values of the same type. They are mutable,
meaning their elements can be modified after creation.