1

I have this property in my class:

var currentPage: Int
    {
        set
        {
            self.currentPage = min(max(0, newValue), self.numberOfPages - 1)

            self.setNeedsDisplay()
        }

        get
        {
            return self.currentPage
        }
    }

I know the problem is in the get, but if I have only:

get
{
    return currentPage
}

The compiler will give me a warning and advises me to add "self" in the front.

Basically, I want to refactor this code from Objective-C:

@interface MyClass

@property (nonatomic, assign) NSInteger currentPage;

@end

@implementation MyClass

- (void)setCurrentPage:(NSInteger)pageNumber
{
    _currentPage = min(max(0, pageNumber), self.numberOfPages - 1);
    [self setNeedsDisplay];
}

- (NSInteger)currentPage
{
    return _currentPage;
}

@end
ppalancica
  • 4,236
  • 4
  • 27
  • 42
  • What is the question or problem that you have? – jscs Nov 08 '14 at 16:34
  • What are you attempting to do, here? Both your setter and your getter cause infinite loops, since they call themselves. – Nate Cook Nov 08 '14 at 16:34
  • Thank you, guys! The problem is that there is infinite loop because of many self calls. I am aware of this, I know how to do it in Objective-C, but in Swift I am not sure. I just want to simply return the value of currentPage. – ppalancica Nov 08 '14 at 16:45
  • Guys, I made an edit to the question, to make it more clear. Thank you for your help! – ppalancica Nov 08 '14 at 16:56
  • I have closed as a duplicate because I think the referenced thread is about the same problem and answers your question. Otherwise leave a comment and I will reopen. – Martin R Nov 08 '14 at 17:10

2 Answers2

1

You need to both add the override keyword to your property declaration and then use super when accessing the stored property of your superclass:

class MyPageControl: UIPageControl {
    override var currentPage: Int {
        get { return super.currentPage }
        set {
            super.currentPage = min(max(0, newValue), self.numberOfPages - 1)
            self.setNeedsDisplay()
        }
    }
}

(Isn't currentPage always going to end up 0 with that logic?)


If you aren't actually overriding currentPage, you'd want to use a didSet handler:

class MyPageControl: UIView {
    var numberOfPages = 6
    var currentPage: Int = 0 {
        didSet {
            self.currentPage = min(max(0, self.currentPage), self.numberOfPages - 1)
            self.setNeedsDisplay()
        }
    }
}
Nate Cook
  • 92,417
  • 32
  • 217
  • 178
  • 1
    I'm not sure the OP is subclassing; sounds more like he's trying to implement the usual Objective-C idiom of using `_currentPage` from a custom getter/setter. – Jesse Rusak Nov 08 '14 at 16:50
  • Thank you, but override gives me compile-time error, because currentPage is a variable I declare in the current class. I just want to create a variable/property like this: "var currentPage: Int" and simply override the setter, the getter should have the default behavior, so which is the best/easiest way to achieve that? – ppalancica Nov 08 '14 at 16:51
  • 1
    @ppalancica The `willSet` and `didSet` observers are the way to do what you want here. See my update and [the documentation](https://developer.apple.com/library/ios/documentation/swift/conceptual/Swift_Programming_Language/Properties.html) for more. – Nate Cook Nov 08 '14 at 17:45
0

If you are overriding currentpage from a base class, you should be using super, not self.

Daniel T.
  • 32,821
  • 6
  • 50
  • 72