NSSharingService: How to compare with the constants? - objective-c

I want to do something when the user shared to
- (void)sharingService:(NSSharingService *)sharingService didShareItems:(NSArray *)items
{
BOOL isSafariReadingList = [sharingService.description rangeOfString:NSSharingServiceNameAddToSafariReadingList].location != NSNotFound;
}
I can't find any property on NSSharingService that I can compare to the constants. Am I missing something?!

Since it seems there's no answer to that (filed http://openradar.appspot.com/16114564), I created a category
#implementation NSSharingService (ActivityType)
- (NSString*)activityType {
NSRange range = [self.description rangeOfString:#"\\[com.apple.share.*\\]" options:NSRegularExpressionSearch];
range.location++; // Start after [
range.length -= 2; // Remove both [ and ]
return [self.description substringWithRange:range];
}
#end
then you can use it these ways:
[[sharingService activityType] isEqualToString:NSSharingServiceNameAddToSafariReadingList];
[#[NSSharingServiceNameAddToSafariReadingList, NSSharingServiceNameAddToIPhoto]
containsObject:[sharingService activityType]];
I guess Apple just didn't think we'd want to know which service people picked from the Picker.
Obviously dangerous to parse that “com.apple.share.System” but it’s the only way I see to avoid a set of rangeOfString || rangeOfString || rangeOfString…..
For more information check out https://github.com/stuffmc/SharingPicker
Also, as a reference, here are some of the values of those NSSharingServiceName* constants, all starting with com.apple.share.
PostOnFacebook Facebook.post
PostOnTwitter Twitter.post
PostOnSinaWeibo SinaWeibo.post
PostOnTencentWeibo TencentWeibo.post
PostOnLinkedIn LinkedIn.post
ComposeEmail Mail.compose
ComposeMessage Messages.compose
SendViaAirDrop AirDrop.send
UseAsTwitterProfileImage Twitter.set-profile-image
UseAsFacebookProfileImage Facebook.set-profile-image
UseAsLinkedInProfileImage LinkedIn.set-profile-image
PostImageOnFlickr Video.upload-image-Flickr
AddToSafariReadingList System.add-to-safari-reading-list
AddToIPhoto System.add-to-iphoto

An equivalent of this check works in my Swift code:
sharingService == NSSharingService(named: NSSharingServiceNameAddToSafariReadingList)

Related

Objective C: Using a BOOL to return 'YES' if a condition can be applied

Hi there I have some code at the moment that gives me the error ("0") is not equal to ("50") - condition not applied correctly Basically I am currently using a traditional for loop within a BOOL which goes through the list of items and checks whether or not the condition can be applied, if it can be applied to an item then the BOOL will return YES. I cannot see where I am currently going wrong and need guidance. My code is shown below:
-(BOOL)conditionCanBeApplied:(NSArray *)items{
bool itemConditionCanBeApplied = NO;
for (int i = 0; i < items.count; i++)
{
id myItem = [[items valueForKeyPath:#"itemId"]objectAtIndex: i];
if ([self.applicableItems containsObject:myItem]) {
itemConditionCanBeApplied = YES;
}
}
return itemConditionCanBeApplied;
}
First, don't mix BOOL and bool, they might be very similar but they aren't the same data type. Second, always use fast enumeration if you have a choice. I am assuming in the code below that your items collection is something like an NSArray. Also, there is no reason to test with an if statement just to set a BOOL since the test is a boolean statement. (I am doing it in my example to allow for the break) Lastly, short-circuiting your logic with a break keeps the processor from doing unnecessary work once you have at least one match.
Do something like this:
- (BOOL)conditionTest
{
BOOL itemConditionCanBeApplied = NO;
for (id item in items) {
if ([self.applicableItems containsObject:item]) {
itemConditionCanBeApplied = YES;
break;
}
}
return itemConditionCanBeApplied;
}

Sort indexPathsForSelectedRows in Swift

When you invoke tableView.indexPathsForSelectedRows(), it returns an array of AnyObject type with the indexPaths of the rows selected in the order that user has selected them.
What I want is to sort the indexPaths in order to get ascending order. Before Swift, it could be achieved with this:
NSArray *sortedIndexPaths = [[tableView indexPathsforSelectedRows]
sortedArrayUsingSelector:#selector(compare:)];
But I have tried to implement it with Swift and it does not work :S
Anyone knows how to deal with this?
Thanks in advance!
it's simple code to sort array of NSIndexPath objects stored in paths variable .
The trick is in casting to [NSIndexPath]. Now you can have your array sorted.
let paths = tableView.indexPathsForSelectedRows() as [NSIndexPath]
let sortedArray = paths.sorted {$0.row < $1.row}
OR
if you wish to have separate function for that like:
func compare (obj0: NSIndexPath, obj1: NSIndexPath) -> Bool {
return obj0.row < obj1.row
}
then
let sortedArray = paths.sorted { compare($0, $1) }
I'm on Windows 7 at the moment so I cannot test but this is what I would expect to work. It may need a type annotation.
let paths = tableView.indexPathsforSelectedRows()
let sorted = paths.sortedArrayUsingSelector("compare:")
You can overload the < and > operators and then just call sort on it.
Define this globally
func <(left: NSIndexPath, right: NSIndexPath) -> Bool {
return left.section < right.section || left.row < right.row
}
Then you can just do this for ascending
let sortedIndexPaths = tableView.indexPathsForSelectedRows?.sort(<)
Obviously because of it returns an optional you would guard against it somehow, for example
guard let sortedIndexPaths = tableView.indexPathsForSelectedRows?.sort(<) else {
return
}
Here is Sort indexPathsForSelectedRows swift 4 code.
if var selection = tableView.indexPathsForSelectedRows
{
selection = selection.sorted{ $1.compare($0) == .orderedAscending }
}
If you have any issue ping me.

RACSignal: how to reduce on arbitrarily large combine

Consider an example (paraphrased) from the ReactiveCocoa Introduction, which
enables based on whether the .password and .passwordConfirm text fields match:
RAC(self.enabled) = [RACSignal
combineLatest:#[ RACAble(self.password), RACAble(self.passwordConfirm) ]
reduce:^(NSString *password, NSString *passwordConfirm) {
return #([passwordConfirm isEqualToString:password]);
}];
Here we know at compile time how many and what things we are combining, and it is useful to destructure/map the "combine" array into multiple arguments to the reduce block. What about when that won't work. For instance, if you want:
RAC(self.enabled) = [RACSignal
combineLatest:arrayOfSignals
reduceAll:^(NSArray *signalValues) { // made this up! don't try at home.
// something ...
}];
How do you do this with ReactiveCocoa?
UPDATE: the accepted answer's comments help explain what I was missing.
You can use map:
RAC(self.enabled) = [[RACSignal combineLatest:arrayOfSignals]
map:^(RACTuple *signalValues) {
// something
}
];
A RACTuple can be manipulated in many ways, it conforms NSFastEnumeration, it has the allObjects method and also the rac_sequence method. You can for example combine all boolean values this way:
RAC(self.enabled) = [[RACSignal combineLatest:arrayOfSignals]
map:^(RACTuple *signalValues) {
return #([signalValues.rac_sequence all:^BOOL(NSNumber *value) {
return [value boolValue];
}]);
}
];
Hope it helps.

Comparing two BOOL values

In my instance method, would like to compare a BOOL parameter with the content of a static variable, for instance:
- (NSArray*)myMethod:(NSString*)someString actualValuesOnly:(BOOL)actualValuesOnly {
static NSString *prevSsomeString;
static BOOL prevActualValuesOnly;
static NSArray *prevResults
if ([someString isEqualToString:prevSomeString] &&
([actualValuesOnly isEqual: prevActualValuesOnly])
// HOW TO COMPARE THESE TWO BOOLEANS CORRECTLY??
{ return prevResults; }// parameters have not changed, return previous results
else { } // do calculations and store parameters and results for future comparisons)
What would be the correct way to do this?
Since BOOL is a primitive (or scalar) type, and not a class, you can compare it directly with ==
if ([someString isEqualToString:prevSomeString] && actualValuesOnly == prevActualValuesOnly)
Boolean variable is compare with == sign instead of isEqual
if(Bool1 == Bool2){
// do something here}
Boolean is compare with == sign instead of isequal:
The solutions mentioned here are not the safest way to compare 2 BOOL values, because a BOOL is really just an integer, so they can contain more than just YES/NO values. The best way is to XOR them together, like detailed here: https://stackoverflow.com/a/11135879/1026573
As Matthias Bauch suggests,
Simply do the comparison using == operator i.e
if (BOOL1 == BOOL2)
{
//enter code here
}

Expected expression before 'unsigned' ->Objective C

m_cAppIdMap is an object of a dictionary.
I want to iterate through the dictionary and to ind and remove the value pEvent.wTimerId is an unsigned short integer that is stored as key in the dictionary.
if(unsigned short* key in m_cAppIdMap) //error:Expected expression before 'unsigned'
{
(void)[self findAndRemoveEvent:pEvent];
(void)CFDictionaryRemoveValue(m_cAppIdMap,&wTimerId);
free(pEvent);
bReturn = YES;
}
I am getting an error when i try to iterate through the loop.
EDITED
-(BOOL)KillTimer:(unsigned short)wTimerId
{
stRs232Timer* pEvent;
BOOL bReturn=FALSE;
theLock = [[NSLock alloc]init];
if ([theLock tryLock]) {
// if ( m_cAppIdMap.Lookup(wTimerId,pEvent) )
// {
// (void)findAndRemoveEvent(pEvent); // remove from event queue
// (void)m_cAppIdMap.RemoveKey(wTimerId); // remove from app map
for(wTimerId in m_cAppIdMap)
{
(void)[self findAndRemoveEvent:pEvent];
(void)CFDictionaryRemoveValue(m_cAppIdMap,&wTimerId);
free(pEvent);
bReturn = YES;
}
[theLock unlock];
}
return bReturn;
}
I am getting error in this code 'selector element does not have a valid object type' . I need to search for wTimerId(key) in the m_cAppIdMap. Is it what i'm doing is correct.The commented lines above the for loop is the implementation of the same code in cpp. I coud not make the same logic over here in Objective C.
I think you meant to use for rather than if. Additionally, the fast enumeration syntax
for (x in y) can only be used on objects that implement the NSFastEnumeration protocol—typically NSArray. It looks like you're using C arrays, so this syntax won't work anyway.
you meant to write for (VARIABLE in CONTAINER) {...} -- but your sample uses if, not for.
side note: it is an error to mutate the collections you iterate over during the iteration.