2

I am new to Swift. I've taken a couple of online course and have started converting an existing Objective-C project to Swift as a learning experience. I have a few questions if someone has time. I've searched the board but haven't found the answers. I am sorry if I missed them.

  1. Delegate Protocols - I'm used to defining my own in Objective-C. Below I have the original Objective-C version and below it I have my new Swift version. Have I followed the correct design pattern for Swift?

  2. I find myself making optionals for all of the properties especially objects like NSData or custom classes. Is this practice acceptable? I am not sure how I would know an initial value for most objects. I know the language wants you to set an initial value but it seems strange for certain objects.

  3. If I am not mistaken we do not have to call self.super init in custom initializers in Swift. Is this correct?

Objective-C Version

@protocol FLOParserHandlerDelegate;

@interface FLOParserHandler: NSObject <NSXMLParserDelegate>

// Properties
@property (nonatomic, strong) NSMutableData *PHData;
@property (nonatomic, strong) NSMutableString *currentParsedCharacterData; // This grabs the       characters as they come in and adds them together.
@property (nonatomic, strong) NSMutableArray *XMLDataArray;  // This is the master array that holds all of the article arrays with the date, title and link objects.
@property (nonatomic, strong) NSMutableDictionary *dateTitleLinkDictionary;// This is used to gather the date, title and link in an array to added to the master array.

// Delegate Property
@property (nonatomic, weak) id <FLOParserHandlerDelegate> delegate;

// init Methods
- (id) initWithCHData: (NSMutableData *) data;

// Class Methods
-(void) startParser;

@end

#pragma mark-
#pragma mark FLOParserHandler Protocol Definition

@protocol FLOParserHandlerDelegate
@optional

- (void) floParserHandlerDidFinishParsing: (FLOParserHandler *) parserHandler;
- (void) floParserHandler: (FLOParserHandler *) parserHandler didFailWithError: (NSError *) error;

@end

Swift Version

import Foundation

protocol FLOParserHandlerDelegate
{
    func floParserHandlerDidFinishParsing(parserHandler : FLOParserHandler) -> ()
    func floParserHandler(parserHandler : FLOParserHandler, error : NSError) -> ()
}

// Note that we have to inherit from NSObject here.  I believe this iis because we are mixing it with Objective-C.
class FLOParserHandler : NSObject, NSXMLParserDelegate
{
    var PHData : NSData?
    var currentParsedCharacterData : String?
    var XMLDataArray : [String]?
    var dateDictionary : [String:NSDate]?
    var titleDictionary : [String:String]?
    var linkDictionary : [String:String]?

    // Delegate Property
    var delegate : FLOParserHandlerDelegate?

    // Init Methods
    init(data : NSData)
    {
        self.PHData = data
    }

    // Class Methds
    func startParser()
    {
        var parser = NSXMLParser(data: self.PHData)
        parser.delegate = self
        parser.parse()
    }
}

Thank you,

Jon

jonthornham
  • 2,881
  • 4
  • 19
  • 35
  • Usually a protocol in objC inherits from the protocol. This is especially important when there are optional methods, because you need to be able to check [delegate respondsToSelector:] which is in the protocol. I don't know how to do it in swift, sorry but I think its an oversight in your original ObjC code.. – Jef Jan 11 '15 at 08:47

1 Answers1

0
  1. Your protocol definition is valid. There is one little thing you should know about:

As with type property requirements, you always prefix type method requirements with the class keyword when they are defined in a protocol

protocol SomeProtocol {
        class func someTypeMethod()
    }
  1. Its perfectly fine to use optionals, or you may use implicity unwrapped optionals like NSData!. In which case you should do this and where no, you may read here: Why create "Implicitly Unwrapped Optionals"?.

Shortly, you doing this in following situations:

a) Constant cannot be defined using initializtion, but you know that it would not be nil (otherwise app will crash)

b) Objective-C Api required you to use pointers, and pointers in Obj-C could be nil. In that case you use imilicity unwrapped optionals.

  1. You always have to call 'super' if you have superclass, to be sure, that class is properly initialized.

Hope that helps.

Community
  • 1
  • 1
Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107