The code works normal for args[0] but doesn't work for args[1]. Can you guys explain this for me or give me a source that I can find out my mistake? Thanks for your help
bot.on('message', message =>{
let args = message.content.substring(PREFIX.length).split(" ");
switch(args[0]){
case 'ping':
message.reply('Pong!');
break;
case 'website':
message.reply('www.facebook.com')
break;
case 'info':
if(args[1] === 'version'){
message.reply('Version ' + version);
}else{
if(args[1] === 'author'){
message.reply('Vikoaz');
}else{
message.reply('Invalid')
}
}
break;
case 'clear':
if(!args[1]) return message.reply('Error')
message.channel.bulkDelete(args[1]);
break;
}
When I write !info version in Discord the bot doesn't reply Version + version. Nothing happens. If just !info the bot replies Invalid.
There are 2 spaces here: .split(" "). Make that 1 space:
let args = message.content.substring(PREFIX.length).split(" ");
With 2 spaces, if message.content is '!info version' args would be ['info version'], which is why it wasn't working.
I have a script:
def tmn_file = ~/.*\.tmn/
def tmc_file = ~/.*\.tmc/
def newTerm = new Properties().with { props ->
new File(inputPath).eachFile(tmn_file) { file ->
file.withReader { reader ->
load( reader )
println "Read data from file $file:"
something read from file...
switch( props.ACTION ) {
case 'NEW':
do something...
}
switch( props.ACTION ) {
case 'CHANGE':
do something...
}
this script looking in directory with path inputPath file with extension tmn_file, which can containt ACTION - NEW or CHANGE.
Script works great, but i want to make another thing:
if file have extension *.tmn (tmn_file) - start only ACTION with NEW case
if file have extension *.tmc (tmc_file) - start only ACTION with CHANGE case
How i can realize decision?
Here is solution:
new Properties().with { props ->
new File(inputPath).eachFile(FileType.FILES) { file ->
file.withReader { reader ->
load(reader)
println "Read data from file $file:"
if (file.name.endsWith('tmn') & props.ACTION == 'NEW' || file.name.endsWith('tmc') & props.ACTION == 'CHANGE') {
// NEW mode
switch( props.ACTION ) {
case 'NEW':
...do someth...
break
// CHANGE mode
case 'CHANGE':
println "***CHANGE mode is on***"
...do someth...
break
default:
throw new RuntimeException("Unknown ACTION $props.ACTION")
}
} else {
if (file.name.endsWith('tmn') || file.name.endsWith('tmc')){
println "$file dont match for action $props.ACTION"
} else {
println "$file have wrong extension "}
}
}
}
}
Using iOS6:
I would like to retrieve the text entered by a user into a UITextField associated with the UIAlertView. I am aware that I could achieve the desired result with a delegate however I am curious about solving this issue with a callback function as I believe this may be an interesting pattern. I began by examining a common pattern for category extension of the UIAlertView class. Code below. Thanks in advance for any suggestions.
import <UIKit/UIKit.h>
#interface UIAlertView (Block)
- (id)initWithTitle:(NSString *)title message:(NSString *)message completion:(void (^)(BOOL cancelled, NSInteger buttonIndex, UITextField *textField))completion cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;
#end
The .m for the category follows:
#import "UIAlertView+Block.h"
#import <objc/runtime.h>
static char const * const alertCompletionBlockTag = "alertCompletionBlock";
#implementation UIAlertView (Block)
- (id)initWithTitle:(NSString *)title
message:(NSString *)message
completion:(void (^)(BOOL cancelled, NSInteger buttonIndex))completion
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ... {
self = [self initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil ];
if (self) {
objc_setAssociatedObject(self, alertCompletionBlockTag, completion, OBJC_ASSOCIATION_COPY);
va_list _arguments;
va_start(_arguments, otherButtonTitles);
for (NSString *key = otherButtonTitles; key != nil; key = (__bridge NSString *)va_arg(_arguments, void *)) {
[self addButtonWithTitle:key];
}
va_end(_arguments);
}
[self setAlertViewStyle:UIAlertViewStylePlainTextInput];
return self;
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
id completion = objc_getAssociatedObject(self, alertCompletionBlockTag);
[self complete:completion index:buttonIndex];
}
- (void) complete:(void (^)(BOOL cancelled, NSInteger buttonIndex))block index:(NSInteger)buttonIndex {
BOOL _cancelled = (buttonIndex == self.cancelButtonIndex);
block(_cancelled, buttonIndex );
objc_setAssociatedObject(self, alertCompletionBlockTag, nil, OBJC_ASSOCIATION_COPY);
//objc_removeAssociatedObjects(block);
}
#end
Usage for the category is set below. The main problem is my inability to reference the UIAlertView textField at Index 0 from within the completion block.
[[[UIAlertView alloc] initWithTitle:#"Add"
message:#"Add New Asset Type"
completion:^(BOOL cancelled, NSInteger buttonIndex){
if (!cancelled) {
//call on completion of UISheetAction ???
NSLog(#"%#",needToAccessUIAlertView._textFields[0]);
}
}
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil] show];
So basically you want to access the alert view from the block. You can do something like this:
__block __weak UIAlertView *alertViewWeak;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Add"
message:#"Add New Asset Type"
completion:^(BOOL cancelled, NSInteger buttonIndex){
if (!cancelled) {
//call on completion of UISheetAction ???
NSLog(#"%#",[alertViewWeak textFieldAtIndex:0]);
}
}
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
alertViewWeak = alertView;
[alertView show];
If you want to make a category by yourself, above is good enough.
But, there are many classes that uses delegation pattern. Do you want to make categories one by one?
There is REKit. With it, you can use that classes as if they were Block-based:
UIAlertView *alertView;
alertView = [[UIAlertView alloc]
initWithTitle:#"title"
message:#"message"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil
];
[alertView
respondsToSelector:#selector(alertView:didDismissWithButtonIndex:)
withKey:nil
usingBlock:^(id receiver, UIAlertView *alertView, NSInteger buttonIndex) {
// Do something…
}
];
alertView.delegate = alertView;
Try this library Here is another useful library to do the same. http://ichathan.com/2014/08/19/ichalertview/
I'm trying to lookup a calendar event by the new iOS method calendarItemWithIdentifier. I can't use the eventWithIdentifier because the identifier is changed after the event is syncronized with the server. The calendarItemIdentifier is not.
But the calendarItemWithIdentifier always returns (null).
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
// Create event.
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = self.title;
event.startDate = [[NSDate date] dateByAddingTimeInterval:3600];
event.endDate = [[NSDate date] dateByAddingTimeInterval:7200];
event.timeZone = [NSTimeZone defaultTimeZone];
event.calendar = [store defaultCalendarForNewEvents];
BOOL success = [store saveEvent:event span:EKSpanThisEvent commit:YES error:&error];
if (success)
{
NSString *calendarItemIdentifier = event.calendarItemIdentifier;
NSLog(#"Assigned identifier: %#", calendarItemIdentifier);
// Look up the event in the calendar.
event = (EKEvent *)[store calendarItemWithIdentifier:calendarItemIdentifier];
if (event) {
NSLog(#"FOUND");
} else {
NSLog(#"NOT FOUND");
}
}
}
}];
From the log:
2013-01-13 10:32:52.042 CalendarIntegration[6095:1303] Assigned identifier: C5FD3792-EBF1-4766-B27D-2767E5C8F3BE
2013-01-13 10:32:52.043 CalendarIntegration[6095:1303] NOT FOUND
Help would be appreciated.
According the doc, this behavior is as expected, link,
A full sync with the calendar will lose this identifier. You should have a plan for dealing with a calendar whose identifier is no longer fetch-able by caching its other properties.
I've created an app which uses the following:
SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook
SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter
The code seems to work OK and makes the posts to Facebook and Twitter but then once the posts have been completed and I return back to the app view none of the controls are active and I have to close the app and relaunch for them to work again.
I think I have nested to code incorrectly in the IF statement (posted below), so was wondering if anybody could offer any advice.
I'm very new to Xcode etc so please be patient and kind to me :-)
Thanks in advance
Pete
- (IBAction)postButton:(id)sender
{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *facebook = [[SLComposeViewController alloc] init];
([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]);
{
SLComposeViewController *twitter = [[SLComposeViewController alloc] init];
facebook = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[facebook setInitialText:[[self statusMessage]text]];
twitter = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[twitter setInitialText:[[self statusMessage]text]];
[self presentViewController:facebook animated:YES completion:nil];
[facebook setCompletionHandler:^(SLComposeViewControllerResult result)
{
NSString *output;
switch (result)
{
case SLComposeViewControllerResultCancelled:
output = #"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Post Sucessfull";
default:
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Facebook" message:output delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[self presentViewController:twitter animated:YES completion:nil];
[twitter setCompletionHandler:^(SLComposeViewControllerResult result)
{
NSString *output;
switch (result)
{
case SLComposeViewControllerResultCancelled:
output = #"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Tweet Sucessfull";
default:
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Twitter" message:output delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}];
}
];}
}
}