简体   繁体   中英

Incompatible pointer types initializing 'dispatch_source_t' (aka 'NSObject<OS_dispatch_source> *') with an expression of type 'NSString *'

Hi I am learner in Objective-c Having a warning of Incompatible pointer types initializing 'dispatch_source_t' (aka 'NSObject<OS_dispatch_source> *') with an expression of type 'NSString *'

- (void)stopAnimating {
    pause = YES;

    if (timerArray) {
        for (NSInteger i = 0; i < [timerArray count]; i++) {
            dispatch_source_t _timer = [[timerArray objectAtIndex:i] source];
            dispatch_source_cancel(_timer);
            _timer = nil;
        }
    
        timerArray = nil;
    }

    [self removeAllFlakesWithAnimation:YES];
}

in dispatch_source_t _timer = [[timerArray objectAtIndex:i] source]; this line, how to solve, timeArray is a NSMutableArray NSMutableArray *timerArray;

We can't tell you what is wrong with your code, there is not enough information for that, but we can tell you what the compiler is doing and why it produces the error it does – then you'll have to resolve it from there.

In your line:

dispatch_source_t _timer = [[timerArray objectAtIndex:i] source];

The LHS declares are variable, _timer , of type dispatch_source_t so the RHS needs to return a value of this type. Let's look at the RHS:

[timerArray objectAtIndex:i]

which BTW you can write more succinctly as:

timerArray[i]

this indexes into an array which you have declared as:

NSMutableArray *timerArray;

the elements of an array like this have type id – which means a reference to any object. The actual type of the objects in the array in this case will not be known until runtime. The next part of the RHS is:

[<a reference so some object> source]

Objective-C allows this and will perform a check at runtime to determine that the reference object does indeed have a method source . However at compile time the compiler can look up the definition of methods called source , it does, and finds that the method returns an NSString * .

So the RHS returns an NSString * and the LHS requires an dispatch_source_t and therefore the compiler reports:

Incompatible pointer types initializing ' dispatch_source_t ' (aka ' NSObject<OS_dispatch_source> * ') with an expression of type ' NSString * '

Now you have to figure out whether you intended to call source or some other method which does return a value of the right type, etc. HTH


As another BTW to someone learning Objective-C: You are using a for loop to produce an index value for an array, and you only use that value to index the array once. A better way to do this is to use a for / in loop:

for (<YourObjectType> element in timerArray) {
   dispatch_source_cancel([element source]);
}

You need to replace <YourObjectType> with the type of object references you've stored in timerArray , and as above the source method needs to return a dispatch_source_t value.

Objective-C has a for ... but there are other really nice ways in which you can iterate through elements of an array. I give one example for array a , in this case of NSString *

    [a enumerateObjectsUsingBlock: ^ ( NSString * i, NSUInteger idx, BOOL * stop ) {

        // do something with NSString * i
        // its index into the array is idx if you need it
        // to exit out of the loop do
        * stop = YES;

    }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM