cs193p – Lecture #6 Protocols and Delegation, Gestures

Please note, this blog entry is from a previous course. You might want to check out the current one.

The sixth lecture contains theory interspersed with demos of the just learned topics.

Extensions

Extensions allow to add methods and properties to classes even if the source is not available. It is not possible to override existing methods and properties. New properties can not be used to store data.

Protocols

Protocols define APIs of methods and properties a caller should provide. It has no storage or implementation associated.

protocol SomeProtocol : class, InheritedProtocol1, InheritedProtocol2 {
    var someProperty: Int { get set }
    func aMethod(arg1: Double, anotherArgument: String) -> SomeType 
    mutating func changeIt()
    init(arg: Type)
}

Continue reading “cs193p – Lecture #6 Protocols and Delegation, Gestures”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Project #2 Assignment #2 Extra Task #3

Please note, this blog entry is from a previous course. You might want to check out the current one.

Add a new method, evaluateAndReportErrors(). It should work like evaluate() except that if there is a problem of any kind evaluating the stack (not just unset variables or missing operands, but also divide by zero, square root of a negative number, etc.), instead of returning nil, it will return a String with what the problem is (if there are multiple problems, you can simply return any one of them you wish). Report any such errors in the display of your calculator (instead of just making it blank or showing some weird value). You must still implement evaluate() as specified in the Required Tasks above, but, if you want, you can have evaluate() return nil if there are any errors (not just in the “unset variable” or “not enough operands” case). The push and perform methods should still return Double? (which is kind of a wasted evaluation, but we want to be able to evaluate your Extra Credit separate from the Required Tasks).

The definitions of the unary and binary operations get new parameters to pass optional error-test methods (and all their occurrences need to be adjusted for the new parameter):
Continue reading “cs193p – Project #2 Assignment #2 Extra Task #3”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Project #2 Assignment #2 Extra Task #2

Please note, this blog entry is from a previous course. You might want to check out the current one.

Add Undo to your Calculator. In Assignment 1’s Extra Credit, you might have added “backspace”. Here we’re talking about combining both backspace and actual undo into a single button. If the user is in the middle of entering a number, this Undo button should be backspace. When the user is not in the middle of entering a number, it should undo the last thing that was done in the CalculatorBrain.

Just for cosmetics change the name of the backspace button to “CE”:

cs193p - Project #2 Assignment #2 Extra Task #2 - Undo Button
cs193p – Project #2 Assignment #2 Extra Task #2 – Undo Button

Continue reading “cs193p – Project #2 Assignment #2 Extra Task #2”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Project #2 Assignment #2 Extra Task #1

Please note, this blog entry is from a previous course. You might want to check out the current one.

Make your description have as few parentheses as possible for binary operations.

Add a new computed variable to the Op enum, which returns by the default the maximum integer value and for binary operations the value you set when defining the operation:

    private enum Op: Printable
    {
        ...
        case BinaryOperation(String, Int, (Double, Double) -> Double)
        ...
        var precedence: Int {
            get {
                switch self {
                case .BinaryOperation(_, let precedence, _):
                    return precedence
                default:
                    return Int.max
                }
            }
        }
    }

Continue reading “cs193p – Project #2 Assignment #2 Extra Task #1”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Project #2 Assignment #2 Task #12

Please note, this blog entry is from a previous course. You might want to check out the current one.

Your UI should look good on any size iPhone in both portrait and landscape (don’t worry about iPad until next week). This means setting up Autolayout properly, nothing more.


… didn’t change anything there either. The two new memory buttons had already be there, just without functionality.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Project #2 Assignment #2 Task #11

Please note, this blog entry is from a previous course. You might want to check out the current one.

When you touch the C button, the M variable should be removed from the variableValues Dictionary in the CalculatorBrain (not set to zero or any other value). This will allow you to test the case of an “unset” variable (because it will make evaluate() return nil and thus your Calculator’s display will be empty if M is ever used without a →M).

… well … we (I) just reset the whole calculator brain by throwing away the old brain and creating a new one. Thus, also the variable values are reset by default. Maybe that was not the intended way to do …

If you have another better solution, please post them in the comments section!

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail