iOS生成随机消息
你好,这是我正在做的iOS项目。 我只是一个编写iOS应用程序的初学者。iOS生成随机消息,ios,objective-c,Ios,Objective C,你好,这是我正在做的iOS项目。 我只是一个编写iOS应用程序的初学者。 我只需要帮助,以便改进>将您的消息放入NSMutableArray中,我认为以下方法对于您的目的来说已经足够好了: + (void) randomOrderedMutableArray:(NSMutableArray *) array { int count = [array count]; for (int i = 0; i < count; ++i) { int nElem
我只需要帮助,以便改进>将您的消息放入NSMutableArray中,我认为以下方法对于您的目的来说已经足够好了:
+ (void) randomOrderedMutableArray:(NSMutableArray *) array
{
int count = [array count];
for (int i = 0; i < count; ++i)
{
int nElements = count - i;
int n = (arc4random() % nElements) + i;
[array exchangeObjectAtIndex:i withObjectAtIndex:n];
}
}
+(void)randomOrderedMutableArray:(NSMutableArray*)数组
{
int count=[数组计数];
对于(int i=0;i
你说的是生成对人类读者有意义的随机句子,因为如果你是,这本身就是一个大学博士研究课题,任何人都无法回答。如果你已经有100个句子,你想随机挑选它们,那就不同了。哪一个?@Martin,我的记事本里有100个句子。我有这样做的想法,但我的线程在AppDelegate.M,NSarray上不断断开,进行100次确认,并使用x=arc4random_uniform。我尝试了很多方法,即使是ipsum,但没有运气:(我仍然在尽我最大的努力使之成为可能。如果一个新手或高年级学生攻克了一个博士研究课题,那么他们就不需要一个博士学位来认可他们的技能。@MartinH,我刚刚包括了100个句子的例子。看一看。我是NSmutable数组的新手。我可以这样做吗:NSmutable数组*affirmation=@[@“你好”,“gg”,@“杀死他们”];?事实上正是这样。让我试着用这个方法。因为我也用谷歌搜索过这样的方法:NSString*a=@“a”;NSMutableArray*array=[NSMutableArray arrayWithObjects:a,nil];因此,我需要将其附加到AppDelegate.m?或viewtodocontroller.m?这很方便,因为当我围绕viewtodocontroller进行调优时,我不断出现线程中断。与其他人一样,我可能有点不清楚,但看起来您想要的是创建一个随机选取的短语列表。您可以在viewDidLoad或ViewDid出现在您的视图控制器中。而不是在AppDelegate中。我得到的错误是“NSarray没有可见的@interface,但我的接口是Assembly。h是接口Assembly:NSObject{NSarray*Assembly;}”
implementation AddToDoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.itemText.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)cancel:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)save:(id)sender {
[self.itemText resignFirstResponder];
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = self.itemText.text;
localNotification.alertAction = @"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self];
// Dismiss the view controller
[self dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.itemText resignFirstResponder];
return NO;
}
@end
NSarray *100affirmations = @[@"Every day in every way I am getting happier and happier.",
@"I am thankful to everybody who has touched my life and made it worth living. ",
@"Happiness is contagious. My happiness makes all these people happy, thus making it one big happy world.",
@"My happy disposition attracts happiness into my life and I only interact with happy people and have only happy experiences.",
@"I spread happiness to others and absorb happiness from others. I enjoy every moment of the day.",
@"Be happy is my motto and happiness is not a destination. It’s my way of life.",
@"I am living my life, feeling motivated and excited about the greatness I am creating, on a daily basis.",
@"I am going to make the best out of my life. I will appreciate all opportunities which are given to me and follow my way.",
+ (void) randomOrderedMutableArray:(NSMutableArray *) array
{
int count = [array count];
for (int i = 0; i < count; ++i)
{
int nElements = count - i;
int n = (arc4random() % nElements) + i;
[array exchangeObjectAtIndex:i withObjectAtIndex:n];
}
}